Answer the question
In order to leave comments, you need to log in
How to specify a foreign key to multiple tables?
Hello! There are several tables: posts, comments, messages, etc. And each has an associated table post_files, comment_files, and so on. But they are essentially identical, and so I decided to combine them into one by adding a field with the type (post, message, ...,) and a common field that will connect (relationship_id). I just can't figure out how to make the foreign key work.
How can this be specified in the context of Laravel? I know how to link with one table
$table->foreign('relationship_id')->references('id')->on('posts');
Answer the question
In order to leave comments, you need to log in
how to make foreign key workNo, Mysql doesn't support it.
In the comments to one of the previous answers, they lied a little tangibly
->foreign () plays a role, and an essential one, because create FK at the DBMS level, which gives:
1. Integrity
2. Indexes
Another thing is that Laravel links can work without FK. That sometimes doesn't matter, and sometimes it doesn't.
And, if I remember correctly, when creating a link in Lara, indexes are not automatically created, then you will most likely need to create them explicitly.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question