[英]Cannot add foreign key constraint, - laravel
我有遷移問題。 表如下。
public function up()
{
Schema::create('users_articles_likes', function (Blueprint $table) {
// $table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('article_id')->unsigned();
$table->foreign('article_id')->references('id')->on('articles');
$table->primary(['user_id', 'article_id']);
$table->timestamps();
});
}
當我嘗試遷移它時。 它沒有推動整個表格。 只需按下user_id
和article_id
這是我在終端顯示的錯誤。
SQLSTATE [HY000]:常規錯誤:1215無法添加外鍵約束(SQL:alter table
users_articles_likes
add constraintusers_articles_likes_user_id_foreign
外鍵(user_id
)引用users
(id
))
用戶表
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
所以問題是因為數據類型錯誤..在你的users表中你有bigIncrements
,它是Big Integer
數據類型,而在另一個表中是integer
試試這個:
$table->bigInteger('user_id')->unsigned();
// or
$table->unsignedBigInteger('user_id');
確保你也查看了article_id
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.