私は注文テーブルを持っており、外部としてsell_shipping_labels
参照しています。orders.id
ただし、Laravel の移行を実行すると、恐ろしいエラー コードが表示されます。
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: 一般エラー: 1005 テーブルを作成できませんcheapbooks_test
。#sql-b5b_b2a
(errno: 150 "外部キー制約の形式が正しくありません") (SQL: alter tablesell_shipping_labels
add constraint Foreign keysell_shipping_labels_order_id_foreign
(order_id
) referencesorders
(id
))[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[HY000]: 一般エラー: 1005 テーブルを作成できませんcheapbooks_test
。#sql-b5b_b2a
(エラー番号: 150 "外部キー制約の形式が正しくありません")
これは私の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');
});
}
今、私は問題を理解しようとして、インターネットをひっくり返しました。この問題に関する投稿はすべて、外部キーを持つテーブルの前に注文テーブルを作成する必要があるという事実に言及していますが、ファイルが正しい順序になっているため、これは問題ではありません。