简体   繁体   English

无法在laravel错误中添加外键约束

[英]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: 现在,我对admin表进行了以下迁移,其中我有一个与我的tags表相关联的外键,迁移如下:

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. 两个表都是innodb我在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 . 因为在tags表中,主键是id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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