我有一个订单表和一个作为外来sell_shipping_labels
引用的。orders.id
但是,当我运行 Laravel 迁移时,我得到了可怕的错误代码:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]:一般错误:1005 无法创建表cheapbooks_test
。#sql-b5b_b2a
(errno: 150 "Foreign key constraint is wrongly forms") (SQL: alter tablesell_shipping_labels
add constraintsell_shipping_labels_order_id_foreign
foreign key (order_id
) 引用orders
(id
))[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[HY000]:一般错误:1005 无法创建表cheapbooks_test
。#sql-b5b_b2a
(errno: 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');
});
}
现在我把互联网颠倒过来试图找出问题所在。所有关于这个问题的帖子都提到必须在具有外键的表之前创建订单表,但这对我来说不是问题,因为我的文件顺序正确。