簡體   English   中英

Laravel 遷移創建具有多個主鍵的表

[英]Laravel migrate creating table with more than one primary key

我發出命令:

php artisan generate:scaffold order --fields="customer_id:integer(11), order_date:date, notes:text”

當它開始遷移時,它以錯誤結束:

General error: 1 table "orders" has more than one primary key (SQL: create table "orders" ("id" integer not null primary key autoincrement, "customer_id" integer not null primary key autoincrement, "order_date" date not null, "notes" text not null))

有一個 Customer 模型和相應的表,我試圖在架構中使用customer_id來引用它。 但是,它不應該是orders表的主鍵。

這是阻止遷移運行的up()代碼:

    Schema::create('orders', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('customer_id', 11);
        $table->date('order_date');
        $table->text('notes');
        $table->timestamps();
    });

我如何讓這個工作?

問題是您將函數->integer()與第二個參數一起使用。 根據文檔,第二個參數需要一個布爾值來設置自動增量與否: bool $autoIncrement = false )。

嘗試這個:

Schema::create('orders', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('customer_id');
        $table->date('order_date');
        $table->text('notes');
        $table->timestamps();
    });

一個可能的問題可能是:

從這里

問題的根源是:帶有外鍵的列必須與該鍵的類型相同。 你有不同的類型:INT/UNSIGNED INT

Laravel 上的文檔也提到了這一點:注意:創建引用遞增整數的外鍵時,請記住始終使外鍵列無符號。

暫無
暫無

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

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