I have below migration for database in Laravel,
public function up()
{
Schema::create('tables', function (Blueprint $table) {
$table->bigIncrements('table_id');
$table->string('table_number');
$table->foreignId('section_id')->constrained('sections')->onUpdate('cascade')->onDelete('cascade');
$table->timestamps();
$table->softDeletes();
});
}
and it throws below error,
SQLSTATE[HY000]: General error: 1005 Can't create table `200120`.`tables` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `tables` add constraint `tables_section_id_foreign` foreign key (`section_id`) references `sections` (`id`) on delete cascade on update cascade)
I am not sure why it references to sections (id), it should reference to sections (section_id)
I did as per this document,
Laravel Doc
Any hints?
you can specify the column name you want to build the relation with:
$table->foreign('section_id')->references('section_id')->on('sections')->onUpdate('cascade')->onDelete('cascade');
Related
I'm trying to create a relationship table between tags and articles, but I have a problem.
migration code:
Schema::create('article_tag', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('article_id');
$table->unsignedBigInteger('tag_id');
$table->timestamps();
$table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
});
the error:
SQLSTATE[HY000]: General error: 1005 Can't create table hiro_blog.article_tag (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table article_tag add constraint article_tag_article_id_foreign foreign key (article_id) references articles (id) on delete cascade)
I have these two migration files one to create a faq_channels table and another to create a faq_questions table. However, I'm not understanding why it is showing an error:
SQLSTATE[HY000]: General error: 1005 Can't create table `test`.`faq_questions` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `faq_questions` add const
raint `faq_questions_channel_id_foreign` foreign key (`channel_id`) references `faq_channels` (`id`) on delete cascade)
Do you know why?
// create_faq_channels_table
public function up()
{
Schema::create('faq_channels', function (Blueprint $table) {
$table->id();
$table->string('channel');
$table->text('description');
$table->timestamps();
});
}
// create_faq_questions_table
Schema::create('faq_questions', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('channel_id');
$table->string('question');
$table->text('response');
$table->timestamps();
$table->foreign('channel_id')->references('id')->on('faq_channels')->onDelete('cascade');
});
I have these two migration files one to create a faq_channels table
and another to create a faq_questions table.
Make sure that migration file for 'faq_channels' table runs before the migration file for 'faq_questions' table. Generally this type of error rises when the parent table is not created before declaring foreign key constraint.
Also edit your migration files according to Talha F.'s answer.
I tried to build a blog and i'm blocked due to an error in the migration of one table.
I've tried to change the order of foreign key creation,
to build the two foreign key into only one with array,
$table->foreign(['commentary_id', 'post_id'])->references(['id', 'post_id'])->on('commentaries');
Commentaries Migration
$table->integer('post_id')->unsigned();
$table->integer('id')->unsigned();
$table->string('content');
$table->integer('user_id')->unsigned();
$table->timestamps();
$table->primary(['post_id', 'id']);
$table->foreign('post_id')->references('id')->on('posts');
$table->foreign('user_id')->references('id')->on('users');
Responses Migration
$table->integer('post_id')->unsigned();
$table->integer('commentary_id')->unsigned();
$table->integer('id')->unsigned();
$table->string('content');
$table->integer('user_id')->unsigned();
$table->timestamps();
$table->primary(['post_id', 'commentary_id', 'id']);
$table->foreign(['commentary_id', 'post_id'])->references(['id', 'post_id'])->on('commentaries');
$table->foreign('user_id')->references('id')->on('users');
And the error that i got :
General error: 1005 Can't create table `blog-lurius`.`responses`
(errno: 150 "Foreign key constraint is incorrectly formed")
(SQL: alter table `responses` add constraint `responses_commentary_id_post_id_foreign` foreign key (`commentary_id`, `post_id`) references `commentaries` (`id`, `post_id`))
I expect to doesn't have a fatal error in my migration. Where my two foreign key are both effective.
Foreign key is not working.
This is my migration file
Schema::create('course_prospect', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('prospect_id')->length(11)->unsigned();
$table->foreign('prospect_id')->references('id')->on('prospect');
$table->integer('course_id')->length(11)->unsigned();
$table->foreign('course_id')->references('course_id')->on('course');
$table->timestamps();
});
I'm getting this error
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error:
1005 Can't create table `customerinquirydb`.`#sql-104c_e9` (errno: 150
"Foreign key constraint is incorrectly formed") (SQL: alter table
`course_prospect` add constraint `course_prospect_prospect_id_foreign`
foreign key (`prospect_id`) references `prospect` (`id`))
When migrating a table with a foreign key in Laravel. The table where that foreign key is, MUST be created first (must exist)! So please make sure that is the case.
I am using laravel 5.4. I want to create relationship with two table. one is users table and another is sir table. users table is already created.
here is my sir table migration code
public function up()
{
Schema::create('sir', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('sir');
}
now when I try to migrate it shows me this error
` SQLSTATE[HY000]: General error: 1005 Can't create table
`finalproject`.`#sq
l-9cc_98` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL:
alter table `sir` add constraint `sir_user_id_foreign`
foreign key (`user_id`) references `users` (`id`))`
I have followed some tutorial but those are not working and also tried with laravel documentation, but still it is not working. anyone please help me to find a solution for this. thanks in advance.