I have laid out an entity relationship model with its corresponding cardinalities, when writing the tables to migrate them in Laravel the following doubt comes to mind, in which table do I place the foreign key, in which I have 1:1 or the one that I have 0:N ?..
EDIT: the statement is as follows, repairs have a client, and a client can have several repairs (or not have any).
I present below two example images with each of the cases:
Foreign key in "repairs" table
Foreign key in "customers" table
I would appreciate a good explanation to know the next time the situation arises... thank you!
For the statement you mentioned, being a many-to-one relationship , the foreign key (in good practice) always travels to the ' many' table .
So for your exercise, the foreign key is in the Repairs table .
For more information on relationships and good practices , consult the links.
I hope I've helped.
Foreign key in "repairs" table
since the repairs in the table must be related to the client that said repair belongs to.
and also you can have records of several repairs that the same client has requested
I hope I have explained myself.
The foreign key must be in the "repairs" table.
In question, the idea of foreign keys is to eliminate the redundancy of data that is presented in the Database, which is why it becomes a number where it is repeated repeatedly, in this case the name of the client within the table "repairs" since the repairs belong to the customers.
Whenever I make a table I ask myself the following question, taking it to the serious pot; How many clients can a repair have? o How many repairs can the customer have? and I answer:
The repairs only belong to one client, therefore I have to assign them the ID of the client to whom I made the repair.
I can make several repairs to clients, therefore it would be inconsistent to assign an ID from the "repairs" table to the "clients" table, this would generate several clients where only one field would vary.
I hope I have resolved your doubts.