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-constraintsFor 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 Migrationshttps://laravel.com/docs/8.x/migrations#generating-migrationsYou 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:
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
TheThe issue is that you're providing the table name asforeignId
method is an alias forunsignedBigInteger
while theconstrained
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 theconstrained
method:
'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
How to Call Curl_Setopt with Curlopt_Httpheader Multiple Times to Set Multiple Headers
Changes to Upload_Max_Filesize in Ubuntu PHP.Ini Will Not Take Effect
Maximum Function Nesting Level of '100' Reached, Aborting After Upgrading to Laravel 5.1
Denormalize Nested Structure in Objects with Symfony 2 Serializer
Checking If String Contains "Http://"
Calling a PHP Function from an HTML Form in the Same File
How to JSON_Encode Array with French Accents
Php-Paypal-Error: 14077410:Ssl Routines:Ssl23_Get_Server_Hello:Sslv3 Alert Handshake Failure
Programming Multi-Language PHP Applications
PHP Class: Global Variable as Property in Class
Reverse Letters in Each Word of a String Without Using Native Splitting or Reversing Functions
How to Assign PHP Array Values to JavaScript Array
How to Access Elements in an Array Returned from a Function