patternMinor
Polymorphic Association - is it bad?
Viewed 0 times
badassociationpolymorphic
Problem
In the following schema:
I'm tempted to have a single
The
The benefits of this is that adding another type is just a matter of expanding the enum. Everything will live on a single table with no redundancy.
The cost seems to be the added complexity on the logic side, which is contained to the one place in my application logic that inserts new entities into the table.
Practically speaking, is there anything wrong with this approach? What would be some other reasons to avoid this?
Collections
Upvotes
Reports
Upvotes
Reviews
UpvotesI'm tempted to have a single
Upvotes table with a single "entityId" column that stores either the CollectionId, ReportId, or ReviewId. There will also be an enum for Type - storing either collection, report, or review. The
entityId will always be required, and the logic will always make sure that inserts enforce uniqueness across each type. The benefits of this is that adding another type is just a matter of expanding the enum. Everything will live on a single table with no redundancy.
The cost seems to be the added complexity on the logic side, which is contained to the one place in my application logic that inserts new entities into the table.
Practically speaking, is there anything wrong with this approach? What would be some other reasons to avoid this?
Solution
I can't recommend polymorphic associations.
In my experience, this will lead to data consistency issues since you can't use a foreign key constraint in the schema. That means you're reliant on your application's writes to this part of the database being bug-free, and no one manually altering the data.
It can also make the queries which join
In my experience, this will lead to data consistency issues since you can't use a foreign key constraint in the schema. That means you're reliant on your application's writes to this part of the database being bug-free, and no one manually altering the data.
It can also make the queries which join
Upvotes to >1 of the other tables more complicated/harder to understand.Context
StackExchange Database Administrators Q#54664, answer score: 6
Revisions (0)
No revisions yet.