I have this migration:
For article
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('category_id')->unsigned();
$table->string('title');
$table->string('slug')->unique();
$table->text('body');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('category_id')
->references('id')
->on('categories')
->onDelete('cascade');
});
}
For category
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
When I try to run the command php artisan migrate I get an error:
General error: 1215 Cannot add foreign key constraint (SQL: alter table`articles` add constraint articles_category_id_foreign foreign key (`category_id`) references `categories` (`id`) on delete cascade)
Several times checked everything I can not find the problem.
Related
Can't create table clothing.clothes (errno: 150 "Foreign key constraint is incorrectly formed")") Laravel 7
Can not assert foreign keys.
Bands
Schema::create('bands', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('band_origin');
$table->text('band_details');
$table->timestamps();
$table->softDeletes();
});
category
Schema::create('category', function (Blueprint $table) {
$table->id();
$table->string('category_name');
$table->string('category_description');
$table->timestamps();
$table->softDeletes();
**gender**
Schema::create('gender', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description');
$table->timestamps();
$table->softDeletes();
**colors**
Schema::create('colors', function (Blueprint $table) {
$table->id('idColor');
$table->String('color_name');
$table->String('color_description')->nullable();
$table->timestamps();
$table->softDeletes();
});
**ERROR HERE TABLE CLOTHES WHERE I WANT to ACQUIRE those field in this table**
Schema::create('clothes', function (Blueprint $table) {
$table->id('clothesID'); //PK
$table->bigInteger('bandID_fk')->unsigned();
$table->bigInteger('categoryID_fk')->unsigned();
$table->bigInteger('genderID_fk')->unsigned();
$table->bigInteger('colorID_fk')->unsigned();
$table->tinyInteger('onStock');
$table->integer('price');
$table->integer('discount')->default(0);
$table->string('description')->nullable();
$table->integer('shoeSize')->nullable();
$table->integer('stock');
$table->string('photo1');
$table->string('photo2')->nullable();
$table->string('photo3')->nullable();
$table->timestamps();
$table->softDeletes();
// ERROR
$table->foreign('bandID_fk')->references('id')->on('bands');
$table->foreign('categoryID_fk')->references('id')->on('category');
$table->foreign('genderID_fk')->references('id')->on('gender');
$table->foreign('colorID_fk')->references('idColor')->on('users');
});
}
This is the right answer
Thank you!
Schema::create('clothes', function
(Blueprint $table) {
$table->id('clothesID'); //PK
$table->bigInteger('bandID_fk')->unsigned();
$table->bigInteger('categoryID_fk')->unsigned();
$table->bigInteger('genderID_fk')->unsigned();
$table->bigInteger('colorID_fk')->unsigned();
$table->tinyInteger('onStock');
$table->integer('price');
$table->integer('discount')->default(0);
$table->string('description')->nullable();
$table->integer('shoeSize')->nullable();
$table->integer('stock');
$table->string('photo1');
$table->string('photo2')->nullable();
$table->string('photo3')->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('bandID_fk')->references('id')->on('bands');
$table->foreign('categoryID_fk')->references('id')->on('category');
$table->foreign('genderID_fk')->references('id')->on('gender');
$table->foreign('colorID_fk')->references('idColor')->on('colors');
I am making migrations where there is a particular migration that maps one table with various other tables using a foreign key. and I am getting the following error while running the migration.
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table bookmaps add constraint bookmaps_subject_id_foreign foreign key (subject_id) references subject (id) on delete cascade)
Here is the migration that is creating the error:
public function up()
{
Schema::create('bookmaps', function (Blueprint $table) {
$table->unsignedBigInteger('book_id')->unique();
$table->unsignedBigInteger('subject_id')->nullable();
$table->unsignedBigInteger('grade_id')->nullable();
$table->unsignedBigInteger('author_id')->nullable();
$table->unsignedBigInteger('catagory_id')->nullable();
$table->unsignedBigInteger('language_id')->nullable();
$table->timestamps();
});
Schema::table('bookmaps', function($table) {
$table->foreign('book_id')->references('id')->on('book')->onDelete('cascade');
$table->foreign('subject_id')->references('id')->on('subject')->onDelete('cascade');
$table->foreign('grade_id')->references('id')->on('grade')->onDelete('cascade');
$table->foreign('author_id')->references('id')->on('author')->onDelete('cascade');
$table->foreign('catagory_id')->references('id')->on('catagories')->onDelete('cascade');
$table->foreign('language_id')->references('id')->on('language')->onDelete('cascade');
});
}
Migrations for other related tables are:
public function up()
{
Schema::create('book', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('discription')->nullable();
$table->string('book_file')->nullable();
$table->string('card_image')->nullable();
$table->unsignedBigInteger('user_id')->nullable();
$table->timestamps();
});
Schema::table('book', function($table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
The table created by above migration is only mapped by foreign key rest of the below migrations are not mapped.
public function up()
{
Schema::create('subject', function (Blueprint $table) {
$table->id();
$table->string('subject_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('grade', function (Blueprint $table) {
$table->id();
$table->string('grade_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('author', function (Blueprint $table) {
$table->id();
$table->string('author_name');
$table->string('author_discription')->nullable();
$table->timestamps();
});
}
public function up()
{
Schema::create('grade', function (Blueprint $table) {
$table->id();
$table->string('grade_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('author', function (Blueprint $table) {
$table->id();
$table->string('author_name');
$table->string('author_discription')->nullable();
$table->timestamps();
});
}
public function up()
{
Schema::create('catagories', function (Blueprint $table) {
$table->id();
$table->string('catagories_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('language', function (Blueprint $table) {
$table->id();
$table->string('language_name');
$table->timestamps();
});
}
What could be the problem?
You have to create the subject migration before the bookmarks. When Laravel tries to migrate the bookmarks table, there is a foreign key linked to the subject table that it does not find.
I'm trying to create a foreign key in the teachers table. after migration, all the columns were there, but the foreign key was not created
First, I have created two tables, which are the users table and the courses table.
if(! Schema::hasTable('users')) {
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->string('password');
$table->string('remember_token')->nullable();
$table->timestamps();
});
}
if(! Schema::hasTable('courses')) {
Schema::create('courses', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('slug')->nullable();
$table->text('description')->nullable();
$table->decimal('price', 15, 2)->nullable();
$table->string('course_image')->nullable();
$table->date('start_date')->nullable();
$table->tinyInteger('published')->nullable()->default(0);
$table->timestamps();
$table->softDeletes();
$table->index(['deleted_at']);
});
}
Then i created another table called 'teachers' with foreign keys
if(! Schema::hasTable('teacher')) {
Schema::create('teacher', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('course_id')->unsigned();
$table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
$table->string('teachers_image')->nullable();
$table->text('education')->nullable();
$table->text('contact')->nullable();
$table->timestamps();
$table->softDeletes();
});
}
after the migration i can see the tables was there but the foreign key was not created
try this
if(! Schema::hasTable('teacher')) {
Schema::create('teacher', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedInteger('course_id');
$table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
$table->string('teachers_image')->nullable();
$table->text('education')->nullable();
$table->text('contact')->nullable();
$table->timestamps();
$table->softDeletes();
});
}
How I drop multiple foreign key and primary keys in migration file.
Bellow is my migration file code.
Migration File
public function up()
{
Schema::create('role_user', function(Blueprint $table){
$table->integer('role_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->primary(['role_id', 'user_id']);
});
}
public function down()
{
Schema::drop('role_user');
//how drop foreign and primary key here ?
}
Blueprint class offers dropForeign and dropPrimary methods that allow you to remove foreign key constraints and primary key.
The following should do the trick:
public function down()
{
Schema::table('role_user', function (Blueprint $table) {
$table->dropForeign('role_user_role_id_foreign');
$table->dropForeign('role_user_user_id_foreign');
$table->dropPrimary();
});
}
Master table POll
public function up()
{
Schema::create('poll', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('poll_question', 255);
$table->boolean('status',1)->default(0)->index();
$table->unsignedInteger('order');
});
}
Detail table
public function up()
{
Schema::create('poll_option', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('poll_id');
$table->string('poll_option', 255);
$table->unsignedInteger('vote_poll_option');
$table->unsignedInteger('order');
$table->boolean('status',1)->default(0)->index();
$table->foreign('poll_id')->references('id')->on('poll')->onUpdate('cascade')->onDelete('cascade');
});
}
When I run php artisan with foreign key
SQLSTATE[HY000]: General error: 1005 Can't create table mydb.#sql-6f4_433 (errno: 150 "Foreign key constraint is incorrectly formed")
Note: I m using laravel 5.2 and mysql type already Innodb What is main causes to incorrectly formed
in Details table
public function up()
{
Schema::create('poll_option', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('poll_id')->unsigned(); //Change Here
$table->string('poll_option', 255);
$table->unsignedInteger('vote_poll_option');
$table->unsignedInteger('order');
$table->boolean('status',1)->default(0)->index();
$table->foreign('poll_id')
->references('id')->on('poll')
->onDelete('CASCADE')
->onUpdate('CASCADE');
});
}
this will work for you