Polymorphic Foreign Keys: Can a Foreign Key Reference Multiple Tables?
The concept of foreign keys in relational databases typically involves specifying the exact target table that a reference column should point to. However, when dealing with polymorphic associations, where a table has a relationship with multiple other tables in a set, the question arises: is it possible to have a foreign key that can reference any one of those tables?
The Answer: No
In both MySQL and PostgreSQL, a foreign key constraint can only reference a single parent table. This constraint ensures data integrity by maintaining a clear relationship between the referencing rows and the referenced rows in the parent table.
Alternatives for Polymorphic Associations
Since a foreign key cannot directly point to multiple tables, alternative solutions are necessary to model polymorphic associations:
Additional Resources
For further insights into polymorphic associations and their solutions, refer to the following resources:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3