C
C
chelkaz2016-03-22 11:05:54
Laravel
chelkaz, 2016-03-22 11:05:54

What does this migration entry mean?

$table->increments('id');
$table->string('owner');
$table->string('image_url');
$table->timestamps();
$table->foreign('owner')
->references('id')
->on('users');
What exactly does it mean:
$table->foreign('owner')
->references('id')
->on('users');

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor, 2016-03-22
@Kraky

The difference is that with a foreign key you will not be able to insert data into the table that is not in the link table in the column with the foreign key, and you will also not be able to delete this entry if this foreign key is used in the link table.
If you specify this in the model, then it will be the same as without the foreign key, but as I said above, you will not be immune from deletions and null insertions. And also for foreign, you can also add a strategy, for example
Which would mean that when a user is deleted, the records from this table that contain the foreign key referring to the user will be deleted.

S
Suinly, 2016-03-22
@Suinly

This is the foreign key implementation through the Laravel table constructor.
Details in the documentation:
https://laravel.com/docs/5.2/migrations#foreign-ke...
https://laravel.ru/docs/v5/schema#external

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question