简体   繁体   中英

Cannot add foreign key constraint in laravel error

I have a migration for my tags table like the below:

Schema::create('tags', function (Blueprint $table) {
            $table->increments('id');
            $table->char('tag' , 15);
});

Now, I have the following migration for my admin table in which i have a foreign key associated with my tags table , the migration is the following:

Schema::create('admin', function (Blueprint $table) {
    $table->char('tag' , 15);
    $table->foreign('tag')->references('tag')->on('tags');
});

Now when i run this migration i get the following error:

在此处输入图片说明

Both the tables are innodb i have changed this in the settings in laravel. But i still get a cannot add a foreign key constraint error. Why ??

Try

$table->foreign('tag')->references('id')->on('tags');

Because in tags table, the primary key is id .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM