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

  • Home
  • SEARCH
  • 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 7564459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:54:34+00:00 2026-05-30T13:54:34+00:00

Polymorphic assiociations (PA’s) is quite a mouthful for a relatively simple database requirement: let

  • 0

Polymorphic assiociations (PA’s) is quite a mouthful for a relatively simple database requirement: let various tables have child records in one shared table. The classic example is a single table with comment records that apply to different not necessarily kindred entities.

In this question Mark did an excellent job showing three common approaches to implement PA’s. I want to use the base table approach, which is described in more detail in an equally excellent answer by Bill Karwin.

A concrete example would look like this:

enter image description here

The primary keys of the entities refer to identical key values in the base table and the Comment table refers to to the base table, so referential integrity is observed. The crucial part here is that the primary keys of the entity tables have distinct domains. They are generated by creating a new record in the base table and copying its generated key to the entity’s primary key.

Now my question: what if I want to introduce PA’s with referential integrity in an existing database having entities that generate their own, mutually overlapping primary keys?

So far, I see two options:

Option 1:

Option 1

Each entity keeps its own primary key but also gets an alternate key.

Like:

  • Close to the recommended approach.
  • Base table is stable.

Dislike:

  • Existing entities must be modified.
  • Hard to find the owning entity of a comment.

Option 2:

Option 2

Each entity has its own foreign key column in the base table. This looks like Mark’s multiple column approach.

Like:

  • Existing entities not affected.
  • Easy to find the owning entity of a comment.

Dislike:

  • Sparse columns
  • Base table not stable: needs modification when a new entity with PA is introduced

I lean to option 1, possibly with a field “EntityName” in the Base table for bidirectional lookup. Which option would be better. Or is another, even better, approach?

  • 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-30T13:54:36+00:00Added an answer on May 30, 2026 at 1:54 pm

    You could use Option 1 but without an additional surrogate Alternate Key.

    Instead, extend the existing Primary Key (of each entity), with an EntityType column (say CHAR(1), that would be E for Events, P for Persons, D for Products).

    The compound (EntityId, EntityType) will become then the Primary Key of table Entity and the corresponding compounds in the other 3 subtype tables.

    (The EntityType is just an auxilary, reference table, with 3 rows):

    Polymorphic_Associations

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

Sidebar

Related Questions

I have 3 tables in my SQL Server database: Teacher Student Parent Each of
I have a polymorphic association in a Ruby on Rails model. In the migration,
Why can you not have a foreign key in a polymorphic association, such as
I am using Polymorphic Models . Simple Question: My code below works without using
I have Comment as a polymorphic model. It is attached to Post, Review, etc.
I have everything on my polymorphic associations working, but if I have model validations,
In my app, I have a unique id for each object (tables). Now, because
I have a Post model that has a polymorphic association with a Vote model:
I am trying to get cancan to work with a polymorphic association, i have
I'm rendering polymorphic records in a view by looping over the polymorphic records and

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.