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.
Related
So im trying to connect 2 tables (users,drawing) in laravel,all of the other columns are being created,the only thing the compiler has problems with is the foreign key,
this is the code for both of the tables
Schema::create('users', function (Blueprint $table) {
$table->id('users_id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->timestamp('email_verified_at')->nullable();
$table->rememberToken();
$table->timestamps();
});
Schema::create('drawings', function (Blueprint $table) {
$table->id('drawings_id');
$table->string('name');
$table->integer('canvas_size');
$table->foreign('users_id')
->references('users_id')->on('users')->onDelete('cascade');
$table->timestamps();
});
and this is the error I get when trying the command(php artisan migrate)
SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'users_id' doesn't exist in table (SQL: alter table `drawings` add constraint `drawings_users_id_foreign` foreign key (`users_id`) references `users` (`users_id`) on delete cascade)
I tried different methods, the old one and new one,neither is working for me,can you please help, also I'm using OpenServer for the database
Solution:My problem was that I was using $table->unsignedInteger('users_id')->nullable();
instead of $table->unsignedBigInteger('users_id')->nullable();
for the users_id,sorry guys my bad
The problem is that you haven't added the user_id column in drawings table.
I will rewrite the two migration files according to the best practice:
Schema::create('users', function (Blueprint $table) {
$table->id(); // You have to leave the primary key "id" so leave this empty
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->timestamp('email_verified_at')->nullable();
$table->rememberToken();
$table->timestamps();
});
Schema::create('drawings', function (Blueprint $table) {
$table->id();// You have to leave the primary key "id" so leave this empty
$table->string('name');
$table->integer('canvas_size');
// You have to add the foreign key column first
$table->unsignedBigInteger('user_id'); // Best to make it single
$table->foreign('user_id')
->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
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.
I want to use two models on my laravel 7.x application : Users and Images :
# Users migration : 2014_10_12_000000_create_users_table.php
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();
});
# Images migration : 2020_03_27_121254_create_models_images_table
Schema::create('images', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned;
$table->string('name');
$table->timestamps();
});
Schema::table('images', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
When trying to migrate i receive following error : General error: 1215 Cannot add foreign key constraint (SQL: alter table images add constraint images_user_id_foreign foreign key (user_id) references users (id) on delete cascade)
I have already searched over google without success, someone could helps me please ?
Thank you
Problem
You are not setting the unsigned properly.
Solution
Replace:
$table->bigInteger('user_id')->unsigned;
By:
$table->bigInteger('user_id')->unsigned();
Explanation
Since the user_id is not unsigned, the definition does not match the id of the users table which means you cannot set the foreign key.
Before Laravel 7.x
You could also do:
$table->unsignedBigInteger('user_id');
Laravel 7.x
As of Laravel 7, you can do it like this in your users migration:
Schema::table('users', function (Blueprint $table) {
$table->id();
// ...
});
And in your images migration:
Schema::table('users', function (Blueprint $table) {
// ...
$table->foreignId('user_id')->constrained();
});
You can find more information here: https://laravel.com/docs/7.x/migrations#foreign-key-constraints
You are missing bracket in unsiged()
As per Laravel Documentation
->unsigned() Set INTEGER columns as UNSIGNED (MySQL)
Change
$table->bigInteger('user_id')->unsigned;
to
$table->bigInteger('user_id')->unsigned();
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.
I have 3 tables to connect each other. Tables name's roles, role_user, and users. I want to make migration on laravel and adding some constraint. and here's what in my role tables migration:
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->timestamps();
});
and here's my Users table migration:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username')->unique();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->boolean('active')->default(0);
$table->softDeletes();
$table->rememberToken();
$table->timestamps();
});
and here's my role_user table migration:
Schema::create('role_user', function (Blueprint $table) {
$table->integer('role_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->unique(['role_id', 'user_id']);
$table->foreign('role_id')->references('id')->on('roles')
->onDelete('cascade')->onUpdate('cascade');
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')->onUpdate('cascade');
});
in my Migration order i put roles table on top of users already but i got this kind of errors:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `role_user` add constraint `role_user_role_id_foreign` foreign key (`role_id
`) references `roles` (`id`) on delete cascade on update cascade)
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
Probably your problem was with the order of creation of migrations.
Laravel runs the migrations in order of creation using the timestamp in the file name , because of that, if you created them in the following order:
roles in 2018_06_02_023539_create_roles_table
role_user in 2018_06_02_023800_create_role_user_table
users in 2018_06_02_023815_create_users_table
The table users would not exist when referenced in the table role_user, thus causing an SQL error.
The most simple fix that you could do is renaming the role_user migration file in this way:
2018_06_02_023800_create_role_user_table => 2018_06_02_023820_create_role_user_table
Try this one on the pivot table
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('role_id');
$table->unsignedBigInteger('user_id');
$table->timestamps();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
I delete all my table and left just 4 tables :
create_user_table,
create_password_reset,
create_roles_table,
create_role_user
and everything works fine.
Till now I don't understand what makes these errors although I already put the same orders.
in table "role_user" replace columns type from integer to bigIncrements