[英]mysql foreign key error in Laravel 5.1
我創建了兩個遷移:
遷移1
Schema::create('responders', function (Blueprint $table) {
$table->increments('id');
$table->string('user_id');
$table->double('latitude', 10, 6);
$table->double('longitude', 10, 6);
$table->timestamps();
});
遷移2
Schema::create('devices', function (Blueprint $table) {
$table->increments('id');
$table->string('user_id');
$table->string('device_id');
$table->string('device_token');
$table->timestamps();
$table->foreign('user_id')
->references('user_id')
->on('responders')
->onDelete('cascade');
});
當我開始遷移時,將拋出錯誤消息:
[照亮\\數據庫\\ QueryException]
SQLSTATE [HY000]:常規錯誤:1215無法添加外鍵約束(SQL:alter tabledevices
add constraint devices_user_id_foreign外鍵(user_id
)引用responders
(user_id
)刪除級聯)
[PDOException]
SQLSTATE [HY000]:常規錯誤:1215無法添加外鍵約束
我特別注意數據類型是一樣的。 在這種情況下,兩者都是string
類型。 為什么無法建立外鍵?
外鍵是引用另一個表的列(通常是主鍵 )的一列(或多列)。 外 鍵的目的是確保數據的引用完整性。 換句話說,只允許出現在數據庫中的值。
responders
表中的user_id
不是一個key
,這就是它顯示此錯誤的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.