【发布时间】:2021-02-21 00:59:11
【问题描述】:
我正在尝试迁移下面的函数,但每次都给我同样的错误,我找不到原因,即使我尝试了文档中的其他语法
//向上函数
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id('cat_id');
$table->string('cat_name');
$table->string('cat_desc');
$table->string('cat_image');
$table->timestamps();
});
Schema::create('sellers', function (Blueprint $table) {
$table->id('seller_id');
$table->string('seller_name');
$table->string('seller_desc');
$table->string('seller_image');
$table->timestamps();
});
Schema::create('products', function (Blueprint $table) {
$table->id('prd_id');
$table->string('prd_name');
$table->string('prd_price');
$table->string('prd_image');
$table->string('prd_desc');
$table->foreignId('product_cat_id')->constrained('categories');
$table->foreignId('product_seller_id')->constrained('sellers');
$table->boolean('prd_status');
$table->timestamps();
});
}
//错误
SQLSTATE[HY000]:一般错误:1005 无法创建表 ecommerce.products (errno: 150 "外键约束不正确 形成”)(SQL:改变表产品添加约束 products_product_cat_id_foreign 外键 (product_cat_id) 参考类别 (id))
我也从数据库中删除了迁移文件并进行了新的迁移,但没有帮助,请指导我,提前谢谢
【问题讨论】:
-
如果您有自定义 ID 名称,请使用更详细的声明 > $table->foreign('user_id')->references('id')->on('users');
标签: php mysql laravel migration laravel-8