G
G
GrimJack2017-08-21 18:00:16
PostgreSQL
GrimJack, 2017-08-21 18:00:16

How to make a connection "on demand"?

Good afternoon
There is a link in the table to itself (linked_record_id refers to record_id)
However, it may not always be. For those who do not have a connection, I want to set linked_record_id = 0
However, I fall with an error -

Foreign key violation: 7 ERROR: insert or update on table "records" violates foreign key constraint "records_linked_record_id_foreign"
DETAIL: Key (linked_record_id)=(0) is not present in table "records". (SQL: insert into "records" ("linked_record_id", "company_id", "form_id", "uuid", "updated_at", "created_at") values (0, 1, 1, c4cf5827-447f-355f-bee9-57c3a7fc84f7, 2017-08-21 14:57:46, 2017-08-21 14:57:46) returning "record_id")

How to be?
As a database - postgres
As a back - laravel 5.4
How to properly connect?
Now the connection is at the db level
h_1503327595_9414218_8ce5222d55.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Dergachov, 2017-08-21
@vanillathunder

$table->...->onDelete('set null');


$table->integer('foreign_id')->unsigned()->nullable();

You need to set not 0, but null.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question