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();
});
Related
I keep getting an error when trying to do a database migration
SQLSTATE[42703]: Undefined column: 7 ERROR: column "user_id" referenced in foreign key constraint does not exist (SQL: alter table "ads" add constraint "ads_user_id_foreign" foreign key ("user_id") references "users" ("id"))
This is my migration file
public function up()
{
Schema::create('ads', function (Blueprint $table) {
$table->id();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('category_id')->references('id')->on('categories');
$table->foreign('city_id')->references('id')->on('cities');
$table->string('title');
$table->text('description');
$table->text('photos');
$table->string('status');
$table->timestamps();
});
}
You need first to create the columns before add foreign key
public function up()
{
Schema::create('ads', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('category_id');
$table->unsignedBigInteger('city_id');
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('category_id')->references('id')->on('categories');
$table->foreign('city_id')->references('id')->on('cities');
$table->string('title');
$table->text('description');
$table->text('photos');
$table->string('status');
$table->timestamps();
});
}
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
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.
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!
I have this four tables but I'm getting the above error in Laravel eloquent. can anyone tell what am I missing here? But when I remove the foreign method the migration works fine.
Schema::create('students', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('first_name');
$table->string('last_name');
$table->string('middle_name');
$table->date('birthdate');
$table->integer('degree_id')->unsigned();
$table->index(['id','first_name', 'last_name']);
$table->timestamps();
$table->foreign('degree_id')
->references('id')
->on('degrees');
});
I included unsigned method but still getting the above error.
Schema::create('degrees', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('description');
$table->string('category_id');
$table->integer('duration');
$table->string('sub_code');
$table->timestamps();
$table->foreign('sub_code')
->references('id')
->on('courses');
});
Here's the other two tables.
Schema::create('instructors', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('first_name');
$table->string('middle_name');
$table->string('last_name');
$table->date('birthdate');
$table->index(['id', 'first_name', 'last_name']);
$table->timestamps();
});
And the last table:
Schema::create('courses', function (Blueprint $table) {
$table->string('id')->unique();
$table->string('description');
$table->integer('no_of_units');
$table->string('room_id');
$table->date('schedule');
$table->integer('instructor_id')->unsigned();
$table->timestamps();
$table->foreign('instructor_id')
->references('id')
->on('instructors');
});
The order of your migration files matters. If you're creating the students table first before the degrees table, your foreign key constraint will fail because the degrees table hasn't been created yet.
To solve this, you can move all your foreign key constraints into a migration create_foreign_key_constraints_migration which should run last.