Cannot find Foreign key constraint index - laravel

my run_tasks table has foreign key completer_guest_id which I need to refer to some other table's column.
Schema::table('run_tasks', function (Blueprint $t) {
$t->integer('completer_id')->nullable();
$t->foreign('completer_id')->references('id')->on('users');
$t->integer('completer_guest_id')->nullable();
$t->foreign('completer_guest_id')->references('id')->on('tasks_guests');
});
But I cannot find the index: run_tasks_completer_guest_id_foreign
because of which I cannot drop the foreign key constraint and delete the column in this migration:
Schema::table('run_tasks', function (Blueprint $table){
$table->dropForeign('run_tasks_completer_guest_id_foreign');
$table->dropColumn('completer_guest_id');
$table->integer('completer_guest_id')->nullable();
$table->foreign('completer_guest_id')
->references('id')
->on('guests')
->onDelete('cascade');
});
and I am getting this error, When I run \d run_tasks I see this
Is there any workaround for this problem?

drop the table and try to separate the creation and the foreign keys assignment
Schema::dropIfExists('run_tasks');
Schema::create('run_tasks', function (Blueprint $table) {
$table->integer('completer_id')->nullable();
$table->integer('completer_guest_id')->nullable();
$table->index(['completer_id', 'completer_guest_id']);
});
Schema::table('run_tasks', function(Blueprint $table) {
$table->foreign('completer_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('completer_guest_id')->references('id')->on('guests')->onDelete('cascade');
});

Related

Laravel migration: integrity constraint violation

I get an error whenever I try to insert values into this table.
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
update a child row: a foreign key constraint fails
(usersexam.role_user, CONSTRAINT role_user_role_id_foreign
FOREIGN KEY (role_id) REFERENCES roles (id) ON DELETE CASCADE)
Here is the migration for that table in my Laravel's database folder:
class CreateRoleUserPivotTable extends Migration
{
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->foreignId('role_id')->references('id')->on('roles')
->cascadeOnDelete();
$table->foreignId('user_id')->references('id')->on('users')
->cascadeOnDelete();
});
}
public function down()
{
Schema::dropIfExists('role_user');
}
}
So I am inserting it into two columns. What is wrong with the table?
UPDATE: here is my insert query
when ever ur creating a tables and refrense a foreign key u still need to make the columns so instead of
Schema::create('role_user', function (Blueprint $table) {
$table->foreignId('role_id')->references('id')->on('roles')->cascadeOnDelete();
$table->foreignId('user_id')->references('id')->on('users')->cascadeOnDelete();
});
it should be something like
Schema::create('role_user', function (Blueprint $table) {
$table->unsignedInteger('role_id');
$table->unsignedInteger('user_id');
$table->foreign('role_id')
->references('id')
->on('roles')
->cascadeOnDelete();
$table->foreign('user_id')
->references('id')
->on('users')
->cascadeOnDelete();
});

Foreign key constraint is incorrectly formed in Laravel-7 migration

When you are applying a foreign key using laravel migration it through this type of error
"Foreign key constraint is incorrectly formed"
The default structure of migration
User Table
---------
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
Chat Table
---------
Schema::create('chats', function (Blueprint $table) {
$table->id();
$table->integer('user_id');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
This is happening because Our column size should not exactly the same size, take a look below.
$table->id();
This will create a big integer
AND
$table->integer('user_id');
This will create a small integer that's why Our foreign key relations fails
How to Fix this issue
$table->unsignedBigInteger('user_id');
OR
$table->foreignId('user_id')->constrained();
Add unsignedBigInteger and your problem will be solved.

Cannot add foreign key constraint, - laravel

I am having migrations issue. Table is below.
public function up()
{
Schema::create('users_articles_likes', function (Blueprint $table) {
// $table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('article_id')->unsigned();
$table->foreign('article_id')->references('id')->on('articles');
$table->primary(['user_id', 'article_id']);
$table->timestamps();
});
}
When I try to migrate it. It doesn't push the whole table. Just pushes the user_id and article_id
and this the error I am displaying in terminal.
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
(SQL: alter table users_articles_likes add constraint
users_articles_likes_user_id_foreign foreign key (user_id)
references users (id))
User table
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
So the problem is because of wrong data type.. In your users table you have bigIncrements which is Big Integer data type, and in the other table integer
So try this:
$table->bigInteger('user_id')->unsigned();
// or
$table->unsignedBigInteger('user_id');
make sure you check the article_id too.

Foreign Key makes Problems in Laravel 5.6

I would like to program an inventory system and need 3 tables for it. These can also be generated via artisan without a foreign key. But as soon as I want to add a foreign key, I get the following error message.
SQLSTATE[HY000]: General error: 1005 Can't create table `inventar`.`#sql-fd4_141` (errno: 150 "Foreign key constraint is incorrectly formed")
(SQL: alter table items add constraint items_lend_foreign foreign key (lend) references lending (id))
Here my Code:
Item Table
Schema::create('items', function (Blueprint $table) {
$table->string('barcode');
$table->primary('barcode');
$table->string('name');
$table->string('description')->nullable();
$table->string('room')->nullable();
$table->string('status')->nullable();
$table->string('annotation')->nullable();
$table->string('image')->nullable();
$table->integer('lend')->unsigned()->nullable();
$table->string('manufactor')->nullable();
$table->timestamps();
});
Schema::table('items',function ($table){
$table->foreign('lend')->references('id')->on('lending');
});
Lending Table
Schema::create('lending', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->integer('personid')->unsigned();
$table->dateTime('startdate');
$table->dateTime('enddate');
$table->timestamps();
});
Schema::table('lending',function ($table){
$table->foreign('barcode')->references('barcode')->on('items');
$table->foreign('personid')->references('personid')->on('persons');
});
Persons-Table
Schema::create('persons', function (Blueprint $table) {
$table->integer('personid')->unsignd()->primary();
$table->string('firstname');
$table->string('lastname');
$table->string('email');
$table->string('annotation')->nullable();
$table->timestamps();
});
I've also googled, but found no solution that works for me.
Is it a problem that my primary key is a string?
Your problem comes in the line
Schema::create('persons', function (Blueprint $table) {
$table->integer('personid')->unsignd()->primary();
}
Change it to unsigned() from unsignd() and it will work fine.

How to create 2 foreign keys with Laravel migration

I have letter's model and this model has 2 foreign keys from attachments and contacts
public function up()
{
Schema::create('letters', function (Blueprint $table) {
$table->increments('id');
$table->integer('contact_id')->unsigned();
$table->integer('attachment_id')->unsigned();
$table->timestamps();
$table->foreign('contact_id')->references('id')->on('contacts');
$table->foreign('attachment_id')->references('id')->on('attachments');
});
}
This is what I have tried so far. But when I type artisan migration SQL I receive the following error
General error: 1005 Can't create table ss.#sql-1064_4a (errno: 150 "Foreign
key constraint is incorrectly formed") (SQL: alter table letters add constraint letters_attach
ment_id_foreign foreign key (attachment_id) references attachments (id))
first create table rows then add forgin keys.
Schema::create('letters', function (Blueprint $table) {
$table->increments('id');
$table->integer('contact_id')->unsigned();
$table->integer('attachment_id')->unsigned();
$table->timestamps();
});
Schema::table('letters', function (Blueprint $table) {
$table->foreign('contact_id')->references('id')->on('contacts');
$table->foreign('attachment_id')->references('id')->on('attachments');
});
i guess this will work

Resources