rade rade
rade rade

Reputation: 189

Laravel foreign key migrate

I'm having some problems with migrations. I already have table, that is not actually migrated with laravel, and I want to make new table with foreign key of the old one. So code looks like this:

Schema::create('games', function (Blueprint $table) {
            $table->bigIncrements('gameid');
            $table->unsignedBigInteger('panelgameid');
            $table->string('name', 255);
            $table->boolean('active');
            $table->float('minslots');
            $table->float('maxslots');
            $table->float('slotincreament');


            $table->foreign('panelgameid')->references('gameid')->on('game');
        });

Everything is setup correctly (I think), I'm receiving error like

SQLSTATE[HY000]: General error: 1005 Can't create table shark_gp.games (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table games add constraint games_panelgameid_foreign foreign key (panelgameid) references game (gameid))

What is the problem here?

Upvotes: 0

Views: 83

Answers (2)

theSlyest
theSlyest

Reputation: 459

You must ensure that the foreign key column panelgameid is the same type as the original key column gameid of the table game. Laravel unsignedBigInteger corresponds to the SQL type UNSIGNED BIGINT, is this the type of gameid?

Upvotes: 1

Ashutosh Kamble
Ashutosh Kamble

Reputation: 193

I think gameid in game table is just integer and not big integer, just make sure it is big integer. If its not then, you probably have to make change into your migration file for panelgameid field like

$table->unsignedInteger('panelgameid');

Upvotes: 0

Related Questions