Antipattern: Use Dual-Purpose Foreign Key

Let’s investigate how using a foreign key to make polymorphic association affects the database.

Defining a Polymorphic Association

To make Polymorphic Associations work, we must add an extra string column alongside the foreign key on issue_id. The extra column contains the name of the parent table referenced by the current row. In this example, the new column is called issue_type and contains either Bugs or FeatureRequests corresponding to the names of the two possible parent tables in this association.

Get hands-on with 1200+ tech skills courses.