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!
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();
});
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 want to create foreign table and product table keys but it gives me error.
1215 Unable to add external index constraints
(SQL: alter table `category_product` add constraint` category
_product_category_id_foreign` foreign key (`category_id`) references` category` (`id`) on delete cascade)
i don't know where exactly is the error.
products table
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->unique();
$table->string('slug')->unique();
$table->string('details')->nullable();
$table->integer('price');
$table->string('description');
$table->timestamps();
});
categories table
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->unique();
$table->string('slug')->unique();
$table->timestamps();
});
category_product table
Schema::create('category_product', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('product_id')->unsigned()->nullable();
$table->foreign('product_id')->references('id')
->on('products')->onDelete('cascade');
$table->integer('category_id')->unsigned()->nullable();
$table->foreign('category_id')->references('id')
->on('category')->onDelete('cascade');
$table->timestamps();
});
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.
After adding $table->engine = 'MyISAM'; seem foreign key is not working anymore. This my main table
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->engine = 'MyISAM';
$table->integer('id');
$table->string('user_id',20);
$table->string('name');
$table->primary(['user_id','id']);
$table->rememberToken();
$table->timestamps();
});
DB::statement('ALTER TABLE users CHANGE id id INT(11) NOT NULL
AUTO_INCREMENT');
}
Foreign key table
public function up()
{
Schema::create('useraccesses', function (Blueprint $table) {
$table->increments('id');
$table->string('user_id',20);
$table->datetime('accessTime')->nullable();
$table->datetime('quittime')->nullable();
$table->datetime('activeHour')->nullable();
$table->text('token')->nullable();
$table->foreign('user_id')
->references('user_id')->on('users')
->onDelete('cascade');
$table->timestamps();
});
}
Thanks
Foreign keys are not supported for MyISAM tables in MySQL. Use InnoDB tables instead. c.f. https://dev.mysql.com/doc/refman/8.0/en/myisam-storage-engine.html
Try this
for users migration
...
$table->increments('id');
...
for useraccesses migration
...
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')
->references('user_id')->on('users')
->onDelete('cascade');
...