簡體   English   中英

Laravel 5.1中的mysql外鍵錯誤

[英]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 table devices add constraint devices_user_id_foreign外鍵( user_id )引用respondersuser_id )刪除級聯)
[PDOException]
SQLSTATE [HY000]:常規錯誤:1215無法添加外鍵約束

我特別注意數據類型是一樣的。 在這種情況下,兩者都是string類型。 為什么無法建立外鍵?

外鍵是引用另一個表的列(通常是主 )的一列(或多列)。 的目的是確保數據的引用完整性。 換句話說,只允許出現在數據庫中的值。

responders表中的user_id不是一個key ,這就是它顯示此錯誤的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM