Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6758083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:44:02+00:00 2026-05-26T13:44:02+00:00

I currently encounter a problem and wonder if there exists a better solution for

  • 0

I currently encounter a problem and wonder if there exists a better solution for this case. Initially I have three entities, A, B, and X. Their relationships are as follows.

  • A-B: 1-to-many
  • A-X: 1-to-many
  • B-X: 1-to-many

For example, one row in A may associate with many rows in both B and X. Also, one row in B may have many other associated rows in X.

Some of the options I have considered:

  1. Duplicate X

    So we have A-B, A-X1, and B-X2 in which X1 and X2 are identical, just different table name.

  2. Use polymorphic association

    X now has two columns parent and parent_id indicating with which table, A or B, a row is associated.

  3. Reverse the polymorphic association

    Two additional tables, AX and BX, are created. The relationship is straightforward.

    A–AX–X–BX–B

  4. Use an additional foreign key for X.

    X now has two columns, A_id and B_id. If a row is associated with A, then B_id is null. If a row is associated with B, then A_id = A.id and B_id = B.id.

Of course they all have tradeoffs. I wonder if there are any better solutions for this problem. Please suggest any one you know.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:44:03+00:00Added an answer on May 26, 2026 at 1:44 pm

    I think #3 is your best option. The common db modelling term for that kind of relationship is Junction Tables. It is a very clean way for creating many-to-many relationships between tables. And as you said:

    The relationship is straightforward.

    With junction tables you don’t need to use duplicates (#1) or create flags indicating which table to link it to (#2) and #4 looks like a slightly different take on #2.

    Many db modelers will bristle at the extra joins but as long as your indexes are set properly the model will be performant. And if you get tired of writing out joins, create some views.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have three independent projects on my plate - each requiring development in
i'm currently experiencing a little problem. I'm using nhibernate with around 40 entities mapped
We currently have a solution developed using SSIS / C#. The SSIS package (amongst
We're currently running with php 5.2.5. We have now encountered a bug that creates
Currently, I don't really have a good method of debugging JavaScript in Internet Explorer and
Currently we have a hybrid ASP/PHP setup connecting to a SQL Server 2005 database.
Currently we have a project with a standard subversion repository layout of: ./trunk ./branches
I have been having a prolonged encounter with the beast known as COM Interop...
I currently encountered a problem: I want to handle adding strings to other strings
I am currently exploring a bug in the template used on http://www.ecovivo.be/rubriek/food . Problem:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.