2017-12-09 18 views
1

私は注文テーブルを持っていて、sell_shipping_labelsは外国人としてorders.idを参照しています。私はLaravelの移行を実行したときしかし、私は恐ろしいエラーコードを取得する:Laravelの移行(errno:150 "外部キー制約が正しく形成されていません")

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table cheapbooks_test . #sql-b5b_b2a (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table sell_shipping_labels add constraint sell_shipping_labels_order_id_foreign foreign key (order_id) references orders (id))

[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table cheapbooks_test . #sql-b5b_b2a (errno: 150 "Foreign key constraint is incorrectly formed")

は、これは私のordersテーブルスキーマです:

Schema::create('orders', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('user_id'); 
     $table->integer('book_id'); 
     $table->integer('status_id'); 
     $table->double('payment_amount')->nullable(); 
     $table->timestamp('received_at')->nullable(); 
     $table->timestamp('paid_at')->nullable(); 
     $table->timestamps(); 
     $table->softDeletes(); 
    }); 

そして、これは私のsell_shipping_labelsスキーマです:今

Schema::create('sell_shipping_labels', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->unsignedInteger('order_id'); 
     $table->string('shippo_object_id'); 
     $table->string('label_url'); 
     $table->string('tracking_url'); 
     $table->string('tracking_number'); 
     $table->timestamp('arrived_at'); 
     $table->timestamps(); 
     $table->softDeletes(); 

     $table->foreign('order_id')->references('id')->on('orders'); 
    }); 
} 

私は問題を理解しようとすると、インターネットを逆さまにした。この問題に関する投稿はすべて、ordersテーブルを作成する必要があるという事実を参照していますBEFOREテーブルに外部キーがありますが、ファイルが正しい順序であるため問題はありません。

答えて

1

increments()が符号なし整数列を作成するので、あなたも符号なし整数として外部キー列を定義する必要があります:私は私を更新しました

$table->integer('order_id')->unsigned(); 

https://laravel.com/docs/5.5/migrations#foreign-key-constraints

+0

$table->unsignedInteger('order_id'); 

かを私はまだ同じエラーコードを取得します。 – FrenchMajesty

+0

@FrenchMajestyあなたはDBを作り直そうとしましたか?もしそうなら、構文が正しいと確信しているので、新しいエラーメッセージを投稿してください。 –

+1

私のデータベースとあなたの答えを削除して再作成するのは大変でした。ありがとうございました! – FrenchMajesty