Solution 1: Simplify the Relationship
Explore how to simplify polymorphic associations by redesigning databases using intersection tables and reverse references. Understand how to enforce data integrity with metadata, avoid common pitfalls, and write efficient queries to manage complex relationships in SQL databases.
We'll cover the following...
It’s better to redesign our database to avoid the weaknesses of Polymorphic Associations but still be able to support the data modeling we need. We will be seeing a few solutions that accommodate the data relationship but make better use of metadata to enforce integrity. These are:
Reverse the reference
One solution to this antipattern is simple once the nature of the problem becomes evident: Polymorphic Associations are backward.
Creating intersection tables
A foreign key in the child table Comments can’t reference multiple parent tables, so, instead, we use multiple foreign keys ...