1
votes

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)

2
Because lkp_cards.lkp_anime_list_id is unsigned and lkp_anime_lists.id isn't. So either remove the ->unsigned() or add it to the lkp_anime_lists $table->id() above,lufc
Initially was without, but still, I re-tested, and I got the same error.Beusebiu
Edit your post to show the code now that the signature of the two columns matches.lufc
Ok, I did that.Beusebiu
The new version of Laravel makes the id() function alias to bigIncrements so you also need to change the $table->integer('lkp_anime_list_id'); to $table->bigInteger('lkp_anime_list_id');lufc

2 Answers

3
votes

You should use

$table->unsignedBigInteger('lkp_anime_list_id')

instead because of primary and foreign keys should be in the same data type

0
votes

This works for me

$table->bigInteger('lkp_anime_list_id')->unsigned();

for Laravel version 6+