I try to migrate db, but I got an error, and I am not sure why. Not sure what is "incorrectly formed".
//First Table
Schema::create('lkp_anime_lists', function (Blueprint $table) {
$table->id();
//more columns here
});
//Second one
Schema::create('lkp_cards', function (Blueprint $table) {
$table->id();
$table->integer('lkp_anime_list_id');
});
Schema::table('lkp_cards', function ($table) {
$table->foreign('lkp_anime_list_id')
->references('id')
->on('lkp_anime_lists')
->onDelete('cascade');
});
SQLSTATE[HY000]: General error: 1005 Can't create table anime_db.lkp_cards (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table lkp_cards add constraint lkp_cards_lkp_anime_list_id_foreign foreign key (lkp_anime_list_id) references lkp_anime_lists (id) on delete cascade)
You should use
$table->unsignedBigInteger('lkp_anime_list_id')
instead because of primary and foreign keys should be in the same data type
This works for me
$table->bigInteger('lkp_anime_list_id')->unsigned();
for Laravel version 6+
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 have a ‘posts’ table and an ‘arrival’ table which references ‘flightno’ (in text string format) as a foreign key. However, when I run the Laravel migration I get the dreaded error:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table atc.#sql-2350_84 (errno: 150 "Foreign key constraint is
incorrectly formed") (SQL: alter table arrival add constraint
arrival_flightno_foreign foreign key (flightno) references posts
(flightno))
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table atc.#sql-2350_84 (errno: 150 "Foreign key constraint is
incorrectly formed")
Posts
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('flightno');
$table->string('flighttype');
$table->string('toa');
$table->string('doa');
$table->string('runway');
$table->string('route');
$table->string('parking');
$table->timestamps();
});
Arrival
Schema::create('arrival', function (Blueprint $table) {
$table->increments('id');
$table->string('flightno');
$table->string('cleaning');
$table->string('rampservice');
$table->string('waste');
$table->string('deicing');
$table->foreign('flightno')->references('flightno')->on('posts')->onDelete('cascade');
$table->timestamps();
});
Seems to me that you forgot to put indexes and set length of flightno columns. This should work:
Posts
Schema::create('posts', function (Blueprint $table) {
// ...
$table->string('flightno', 30)->index();
// ...
});
Arrival
Schema::create('arrival', function (Blueprint $table) {
// ...
$table->string('flightno', 30)->index();
// ...
});
In the foreign key column use unsignedBigInteger to avoid mismatch foreign key data type problem.
For example in your arrival table use the type of flightno as unsignedBigInteger.
Posts:
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('flightno');
$table->string('flighttype');
$table->string('toa');
$table->string('doa');
$table->string('runway');
$table->string('route');
$table->string('parking');
$table->timestamps();
});
Arrival:
Schema::create('arrival', function (Blueprint $table) {
$table->increments('id');
//---Change flightno type to unsignedBigInteger and if u faced any problem just use nullable at the end of flightno.
$table->unsignedBigInteger('flightno');
$table->string('cleaning');
$table->string('rampservice');
$table->string('waste');
$table->string('deicing');
$table->foreign('flightno')->references('flightno')->on('posts')->onDelete('cascade');
$table->timestamps();
});
It will solved the problems of a lot of people that faced this problem.
I have letter's model and this model has 2 foreign keys from attachments and contacts
public function up()
{
Schema::create('letters', function (Blueprint $table) {
$table->increments('id');
$table->integer('contact_id')->unsigned();
$table->integer('attachment_id')->unsigned();
$table->timestamps();
$table->foreign('contact_id')->references('id')->on('contacts');
$table->foreign('attachment_id')->references('id')->on('attachments');
});
}
This is what I have tried so far. But when I type artisan migration SQL I receive the following error
General error: 1005 Can't create table ss.#sql-1064_4a (errno: 150 "Foreign
key constraint is incorrectly formed") (SQL: alter table letters add constraint letters_attach
ment_id_foreign foreign key (attachment_id) references attachments (id))
first create table rows then add forgin keys.
Schema::create('letters', function (Blueprint $table) {
$table->increments('id');
$table->integer('contact_id')->unsigned();
$table->integer('attachment_id')->unsigned();
$table->timestamps();
});
Schema::table('letters', function (Blueprint $table) {
$table->foreign('contact_id')->references('id')->on('contacts');
$table->foreign('attachment_id')->references('id')->on('attachments');
});
i guess this will work
I have couples of tables Client, Rooms, and Reservations. When i try to migrate I got this kind of errors:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table `laravel_london`.`#sql-142c_6f` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table
`reservations` add constraint `reservations_client_id_foreign` foreign key (`client_id`) references `clients` (`id`))
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table `laravel_london`.`#sql-142c_6f` (errno: 150 "Foreign key constraint is incorrectly formed")
here's my reservation migration code:
public function up()
{
Schema::create('reservations', function (Blueprint $table) {
$table->increments('id');
$table->date('date_in');
$table->date('date_out');
$table->integer('client_id')->unsingned;
$table->foreign('client_id')->references('id')->on('clients');
$table->integer('room_id')->unsingned;
$table->foreign('room_id')->references('id')->on('rooms');
$table->timestamps();
});
}
some said that it's because of my migration order, but I think it's already in the right order and here's the screenshot of it:
so how can i can migrate it without any errors? thanks.
Your spelling is wrong.. unsigned not unsingned and you forget to use ()
public function up()
{
Schema::create('reservations', function (Blueprint $table) {
$table->increments('id');
$table->date('date_in');
$table->date('date_out');
$table->integer('client_id')->unsigned();
$table->foreign('client_id')->references('id')->on('clients');
$table->integer('room_id')->unsigned();
$table->foreign('room_id')->references('id')->on('rooms');
$table->timestamps();
});
}
why laravel schema reply
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table test.#sql-13cc_d0 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table cities add constrai
nt cities_provinces_id_foreign foreign key (provinces_id) references provinces (id) on delete cascade)
[PDOException] SQLSTATE[HY000]: General error: 1005 Can't create
table test.#sql-13cc_d0 (errno: 150 "Foreign key constraint is
incorrectly formed")
first table
Schema::create('provinces', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->boolean('is_enable');
$table->boolean('is_deletable');
$table->boolean('is_editable');
$table->boolean('deleted');
$table->timestamps();
});
second table
Schema::create('cities', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('province_id')->unsigned()->index();
$table->boolean('is_enable');
$table->boolean('is_deletable');
$table->boolean('is_editable');
$table->boolean('deleted');
$table->timestamps();
$table->foreign('province_id')
->references('id')->on('provinces')
->onDelete('cascade');
});
You should make sure, your provinces table migration is running before cities table migration.
You should change provinces or cities migration file name to make sure timestamp at the beginning of provinces table migration file will be before cities table.
The time go ahead but i will comment something else.
Andile is right! You must have the same type. In this case, the foreign key should have the type bigInteger because you have bigIncrements on the reference table.
I think thats the solution for a little and boring debug. (sorry for my english)
Try to create table first and then add constraint:
Schema::create('cities', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('province_id')->unsigned()->index();
$table->boolean('is_enable');
$table->boolean('is_deletable');
$table->boolean('is_editable');
$table->boolean('deleted');
$table->timestamps();
});
Schema::table('cities', function (Blueprint $table) {
$table->foreign('province_id')->references('id')->on('provinces')->onDelete('cascade');
});
The foreign key and referencing table must be same type, the auto generated id are of bigInteger, so instead of defining your column as Integer use bigInteger insteated. That Solved the problem for me