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.
Related
I have this error when run migration
SQLSTATE[HY000]: General error: 1005 Can't create table tms-app.#sql-1e64_2b (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table projects add constraint projects_cout_id_foreign foreign key (cout_id) references couts (id) on update cascade)
this is projects table:
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('libelle');
$table->string('libelle_long');
$table->string('direction');
$table->integer('cout_id')->unsigned();
$table->foreign('cout_id')
->references('id')->on('couts')
->onUpdate('cascade');
$table->foreign('status')
->referenecs('id')->on('statuses')
->onUpdate('cascade')
->onDelete('cascade');
$table->timestamps();
});
Sometimes this error happens, when we define foreign key and you should use bigInteger('') or use unsignedBigInteger('').
Use the code below:
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('libelle');
$table->string('libelle_long');
$table->string('direction');
$table->bigInteger('cout_id')->unsigned();
$table->bigInteger('status')->unsigned();
$table->foreign('cout_id')->references('id')->on('couts')->onDelete('cascade');
$table->foreign('status')->references('id')->on('statuses')->onDelete('cascade');
$table->timestamps();
});
Note: In tables couts & statuses change the id field $table->increments('id'); to $table->bigIncrements('id');
This error should be casuses by the following miss-configuration below I mentioned, be sure all of them to be configured correctly
1: Laravel migration foriegn key should be assgined as bigInteger or unsignedBigInteger
example:
$table->unsignedBigInteger('user_id');
or
$table->bigInteger('user_id')->unsigned();
and for assigning foriegn key attribute you should do
$table->foriegn('user_id')->reference('id')->on('users'); // if you need further attribtute you can chain to this line
2: be sure that the order of migration file is formed correclty because Laravel will migrate table based on timestamp for example if need a foreign key from user table first you need to create user table then the child table the timestamp of users table should be older than child (even 1 second)
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 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');
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.
I currently have two tables Symbols and Prices. I am currently creating a migration for Prices and adding a FK constraint to Prices.symbol_id which references Symbols.id. This is the Prices migration:
$table->increments('id');
...
...
$table->integer('symbol_id');
$table->foreign('symbol_id')
->references('id')->on('Symbols')
->onDelete('cascade’);
Symbols.id is simply a $table->increments(‘id’);
However, when I run the migration, this is what happens:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter
table `Prices` add constraint prices_symbol_id_foreign foreign key (`symbol_id`) re
ferences `Symbols` (`id`))
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
Any ideas why?
make a new migration
php artisan make:migration update_prices_table
The schema:
public function up()
{
Schema::table('Prices', function (Blueprint $table) {
$table->integer('symbol_id)->unsigned();
$table->foreign('symbol_id')
->references('id')->on('Symbols')
->onDelete('cascade’);
});
}
If your primary key is of type bigIncrements , make sure you use bigInteger as datatype for the foreign Key