Cannot add foreign key constraint in Laravel Migrations - laravel

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

Related

Laravel database migration foreign constrained

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');

How to make relation between user and category tables in laravel?

After trying all of this possibility, still, have this error message:
SQLSTATE[HY000]: General error: 1824 Failed to open the referenced table 'categories'
(SQL: alter table `users` add constraint `users_category_id_foreign` foreign key
(`category_id`) references `categories` (`id`) on delete cascade)
The code of relation in users migration:
$table->unsignedBigInteger('category_id');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
The image of the table:
Help :(
Remove those lines from the create_users_table migration and add this to the create_categories_table.
// function up() below the Schema::create('categories'..
Schema::table('users', function (Blueprint $table) {
$table->unsignedBigInteger('category_id')->nullable();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
I made it nullable because again, you are adding it to an existing table, so if you happen to have any users in the table, the migration will fail again if there is no default value.

Migration : Two foreign key referenced to the same table result to error

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 when I migrate

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.

Laravel lumen another table id relationship

What am I doing wrong?
option-migration file
Schema::create('option', function (Blueprint $table) {
$table->increments('id');
$table->integer('contents_content_id')->unsigned();
$table->timestamps();
$table->foreign('contents_content_id')
->references('content_id')
->on('contents');
});
contents-migration-file
Schema::create('contents', function (Blueprint $table) {
$table->increments('id');
$table->integer('content_id');
$table->timestamps();
});
error
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table option add constraint option_content_content_id_foreign foreign key (content_content_id) references contents (content_id))
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
The problem is that the option table get created before the contents table, and when you reference content_id on content, content table doesn't exist yet.
Note that :
Each migration file name contains a timestamp which allows Laravel to determine the order of the migrations.
So what you have to do now, is to delete the migrations from database/migrations folder and re-create them in order : first options then contents.

Resources