B
B
BonBon Slick2017-10-01 14:39:08
Database
BonBon Slick, 2017-10-01 14:39:08

To add foreign_key on the current table from which we add a key?

Referral system, table of users id | referral_id . When trying to put a foreign key on the same table

$table->foreign('refferral_id')->references('id')->on('users')->onDelete('set null');

Gives an error message:
nvalid foreign key: 7 ERROR:  there is no unique constraint matching given keys for referenced table

That's right, referral_id cannot be unique.
Therefore, the question arose, is it possible at all? Or would it be better to do it through a pivot table?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question