在laravel中使用外键迁移时出错

Posted

技术标签:

【中文标题】在laravel中使用外键迁移时出错【英文标题】:error while migrating with foreign key in laravel 【发布时间】: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'); 【参考方案1】:
public function up()
    
        Schema::create('categories', function (Blueprint $table) 
            $table->increments('id');
            $table->integer('cat_id')->unsigned();
            $table->string('cat_name');
            $table->string('cat_desc');
            $table->string('cat_image');
            $table->timestamps();
        );



Schema::create('sellers', function (Blueprint $table) 
        $table->increments('id');
        $table->integer('seller_id')->unsigned();
        $table->string('seller_name');
        $table->string('seller_desc');
        $table->string('seller_image');
        $table->timestamps();
    );

    Schema::create('products', function (Blueprint $table) 
        $table->increments('id');
        $table->integer('prd_id')->unsigned();
        $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();
    );


【讨论】:

【参考方案2】:

这是因为,在您的categoriessellers 两个表中,主键 都不是id,默认情况下constrained 方法使用@987654326 @ 作为主键作为外键引用,所以改变:

$table->foreignId('product_cat_id')->constrained('categories');
$table->foreignId('product_seller_id')->constrained('sellers');

到,

$table->unsignedBigInteger('product_cat_id');
$table->foreign('product_cat_id')->references('cat_id')->on('categories');

$table->unsignedBigInteger('product_seller_id');
$table->foreign('product_seller_id')->references('seller_id')->on('sellers');

如果您使用的是模型,那么还要在您的模型上定义您的 primary key,例如:

protected $primaryKey = 'cat_id';

【讨论】:

以上是关于在laravel中使用外键迁移时出错的主要内容,如果未能解决你的问题,请参考以下文章

laravel 和 artisan 错误无法在迁移时添加外键

LARAVEL 8:一般错误:使用外键运行迁移时发生 1005

无法使用 laravel 数据库迁移创建外键

无法在 Laravel 迁移中添加外键约束

如何在 laravel 5.1 迁移中使用外键

Laravel 迁移自引用外键问题