Laravel Errno 150 Foreign Key Constraint Is Incorrectly Formed

How to fix error on Foreign key constraint incorrectly formed in migrating a table in Laravel

@JuanBonnett’s question has inspired me to find the answer. I used Laravel to automate the process without considering the creation time of the file itself. According to the workflow, “meals” will be created before the other table (categories) because I created its schema file (meals) before categories.
That was my fault.

Laravel migration (errno: 150 Foreign key constraint is incorrectly formed)

Since increments() creates an unsigned integer column, you need to define the foreign key column as unsigned integer too.

Default migrations in Laravel 6+ use bigIncrements(), so you need to use unsignedBigInteger() method:

$table->unsignedBigInteger('order_id');

https://laravel.com/docs/6.x/migrations#foreign-key-constraints

For default migrations in older versions of Laravel use unsignedInteger() method:

$table->unsignedInteger('order_id');

Or:

$table->integer('order_id')->unsigned();

https://laravel.com/docs/5.5/migrations#foreign-key-constraints

laravel 8 (errno: 150 Foreign key constraint is incorrectly formed)

The problem is that your migration for the posts table is run prior to your discussions migration.

This happens because Laravel runs the migrations ordered by the timestamp in the migrations file name:

2021_11_13_000535_create_posts_table -> 13. November
2021_11_19_165302_create_discussions_table -> 19. November

Therefore the dicsussions table is not created yet as my comment suggested!

The solution is easy, change the file name to:

2021_11_20_000535_create_posts_table -> 20. November

Next time please take a look into your DB as I suggested earlier.

From their documentation:

Generating Migrations

You may use the make:migration Artisan command to generate a database
migration. The new migration will be placed in your
database/migrations directory. Each migration filename contains a
timestamp that allows Laravel to determine the order of the
migrations:

https://laravel.com/docs/8.x/migrations#generating-migrations

Laravel - errno: 150 Foreign key constraint is incorrectly formed in migration

All your foreign key need to be unsigned, change bigInteger to unsignedBigInteger

primary and foreign references must be of same type. bigIncrements() needs unsignedBigInteger() and increments() needs unsignedInteger()

(Laravel 8) errno: 150 Foreign key constraint is incorrectly formed

Please read the documentation first.

https://laravel.com/docs/8.x/migrations#foreign-key-constraints

The foreignId method is an alias for unsignedBigInteger while the constrained method will use conventions to determine the table and column name being referenced. If your table name does not match Laravel's conventions, you may specify the table name by passing it as an argument to the constrained method:

The issue is that you're providing the table name as 'cascade' instead of 'users'.

i.e

//Should be...
$table->foreignId('user_id')->constrained('users');
//Instead of...
$table->foreignId('user_id')->constrained('cascade');

Don't forget to correct the 'category_id' as well.

If you really wish to apply 'cascade' options, try:

$table->foreignId('user_id')
->constrained()
->onUpdate('cascade')
->onDelete('cascade');

laravel errno 150 foreign key constraint is incorrectly formed

In case of foreign keys, the referenced and referencing fields must have exactly the same data type.

You create the id fields in both users and firms as signed integers. However, you create both foreign keys as unsigned integers, therefore the creation of the keys fail.

You need to either add the unsigned clause to the id field definitions, or remove the unsigned clause from the foreign key fields.



Related Topics



Leave a reply



Submit