J
J
jazzus2019-05-13 04:30:03
Laravel
jazzus, 2019-05-13 04:30:03

Why is the key not being added?

Migration

$table->bigIncrements('id');
$table->integer('user_id')->unsigned();
$table->integer('project_id')->unsigned();
$table->integer('referral_id')->unsigned();
$table->timestamps();

$table->foreign('user_id')->references('id')->on('users')->onDelete('CASCADE');
$table->foreign('project_id')->references('id')->on('projects')->onDelete('CASCADE');
$table->foreign('referral_id')->references('id')->on('referrals')->onDelete('CASCADE');

Writes you can not add a key to referral_id. At the same time, referrals exists, there is an id and the table is created with the first two keys. There is no third. Check what?
referrals table
Schema::create('referrals', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name', 100);
});

error text
General error: 1215 Cannot add foreign key constraint (SQL: alter table `project_referrals` add constraint `project_referrals_referral_id_foreign` foreign key (`referral_id`) references `referrals` (`id`) on delete CASCADE)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jazzus, 2019-05-13
@jazzus

I found the answer on a toaster . Thanks Ilya

A
Alexander Aksentiev, 2019-05-13
@Sanasol

Make referral_id nullable or remove foreign link

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question