Cant add foreign key in Laravel - laravel

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

Related

How to declare a foreign key in laravel

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();
});

Laravel Migration: Creating 2 tables Users Table & Advertisements Table (with foreign key referring to user's id)

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');
});

Foreign key constraint is incorrectly formed. Not able to add string foreign key

I am trying to create string foreign key constraint. Nut nothing is working.
Accounts table-
Schema::create('accounts', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->string('customer_id');
$table->foreign('customer_id')->references('customer_id')->on('customers');
$table->string('ac_type_id');
$table->string('ac_number')->primary();
$table->string('ac_balance');
$table->string('password');
$table->timestamps();
});
Customers table-
Schema::create('customers', function (Blueprint $table) {
$table->string('customer_id')->primary();
$table->string('first_name');
$table->string('last_name');
$table->string('mobile_no');
$table->string('email_id');
$table->string('address');
$table->date('dob');[enter image description here][1]
$table->timestamps();
});
This is error I'm getting always

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.

How to foreign key in Laravel 5.3

I use Laravel 5.3 and MySQL
I will to make foreign key on Forums and references on user id
Migration in Users
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('api_token', 60)->unique();
$table->rememberToken();
$table->timestamps();
});
}
Migration in Forums
{
Schema::create('forums', function(Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('thread');
$table->text('deskripsi');
$table->timestamps();
});
Schema::table('forums', function(Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users');
});
}
But I get found error
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`dbkelas`.`forums`, CONSTRAINT `forums_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) (SQL: insert into `forums` (`thread`, `deskripsi`, `updated_at`, `created_at`) values (Matematika Diskrit, Bagaimana ya, 2017-01-14 15:10:18, 2017-01-14 15:10:18))
Do you have any solution?
I think your migration code need to update like:
Schema::create('forums', function(Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->string('thread');
$table->text('deskripsi');
$table->timestamps();
});
Hope this work for you!

Resources