我有一个订单表和一个sell_shipping_labels引用orders.id作为外国。但是,当我运行Laravel迁移时,我得到了可怕的错误代码:
[照亮\数据库\ QueryException]
SQLSTATE [HY000]:常规错误:1005无法创建表cheapbooks_test.#sql-b5b_b2a(错误:150“外键约束形成错误”)(SQL:alter table sell_shipping_labels add constraint sell_shipping_labels_order_id_foreign foreign key(order_id)references orders(id))
[学说\ DBAL \驱动\ 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');
});
}
现在我已经颠倒了互联网,试图找出问题所在。关于这个问题的所有帖子都提到了这样一个事实,即必须在具有外键的表之前创建订单表,但这对我来说不是问题,因为我的文件的顺序正确。