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();
});
Related
I'am trying to create Advertisement table which contain 'user_id' column referencing to 'id' column in users table, but with no luck.
as of the error message, i noticed that framework not passing column 'id' of the users table. Knowing i manged to solve this issue manually by using "phpMyAdmin".
I am wondering if there is any other way to solve this issue?
Users Schema:
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();
});
Advertisements Schema:
Schema::create('advertisements', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('name');
$table->text('description');
$table->mediumtext('image')->nullabe;
$table->timestamps();
$table->foreign('user_id')
->reference('id')
->on('users')
->onDelete('cascade');
});
Error:
PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') on delete cascade' at line 1").
PDO::prepare("alter table `advertisements` add constraint `advertisements_user_id_foreign` foreign key (`user_id`) references `users` () on delete cascade")
the problem is in your Advertisements migration code, you should put it ->references('id') instead of ->reference('id')
Advertisements Schema:
Schema::create('advertisements', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('name');
$table->text('description');
$table->mediumtext('image')->nullabe;
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
I am running this migration:
Schema::create('logs', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('key');
$table->text('value')->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
"Users" table migration (migrated at Laravel 5.8)
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('last_message_id')->nullable();
$table->timestamps();
});
Error that I receive:
Foreign key constraint is incorrectly formed
To me it seems like you ran the users migration with a previous version of laravel. Have a look with a db tool (like phpmyadmin, Sequel, etc.) what the actual data type of your users.id field is.
I assume it's not bigInteger so your solution could be to just use
$table->unsignedInteger('user_id');
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();
When I try to migrate I get an error "Foreign key constraint is incorrectly formed", but I cant see why
1st table
Schema::create('uciteles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->integer('admin');
$table->string('predmet');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->rememberToken();
$table->timestamps();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
2nd table
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->rememberToken();
$table->timestamps();
});
i think here first you create uciteles table then users table.
so here change that format.first create users table & then uciteles table.
change the time of created table in migration like this 2019_09_27_000000_create_users_table.php change date & month then users table create first & secondly the uciteles table or disble foreign key check & enable it
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
Schema::create('uciteles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->integer('admin');
$table->string('predmet');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->rememberToken();
$table->timestamps();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
The type of the column must be the same as in the foreign key reference and you need to create the column yourself.
Here are the explanations to your question : Creating Foreign Key in Laravel Migrations
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.