Laravel:使用主索引作为外键? [英] Laravel: use primary index as foreign key?

查看:112
本文介绍了Laravel:使用主索引作为外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模型user和模型profilePresi之间具有hasOne关系.这意味着每个用户都有0个或1个总裁设置.因此,profilePresi表的主索引与外键相同.

I have a hasOne relation between the model user and the model profilePresi. This means each user has either 0 or 1 president settings. Thus, the primary index of the profilePresi table is identical to the foreign key.

这就是我要添加外键的方式:

This is how I wanted to add the foreign key:

Schema::create('profilePresi', function (Blueprint $table) {
          $table->integer('idMember');
          $table->primary('idMember');
          $table->string('idCountry',2);
          $table->timestamps();
});

Schema::table('profilePresi', function($table) {
          $table->foreign('idMember')
                ->references('id')->on('users')
                ->onDelete('cascade');
});

但是我遇到以下错误:

我该如何解决?我正在使用InnoDB引擎,所以我知道可以将索引键设置为外键.

How can I fix it? I am using InnoDB engine so I know that I am allowed to set index key as foreign key.

推荐答案

您的users.id列可能是无符号整数,但是您的profilePresi.idMember只是一个整数.要用作外键,它们都必须是未签名的或未签名的.检查是否是这种情况,如果是,只需使profilePresi.idMember也未签名-应该这样做. :)

Your users.id column is likely an unsigned integer, but your profilePresi.idMember is just an integer. To use as a foreign key, they both need to be either unsigned or not. Check if that's the case, and if so, just make the profilePresi.idMember unsigned as well - that should do it. :)

这篇关于Laravel:使用主索引作为外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆