【问题标题】:error while migrating with foreign key in laravel在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');

标签: php mysql laravel migration laravel-8


【解决方案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】:

    这是因为,在您的categories 和sellers 两个表中,主键 都不是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';
    

    【讨论】:

      猜你喜欢
      • 2016-04-02
      • 2014-04-03
      • 2016-05-13
      • 2019-01-31
      • 2014-09-27
      • 2019-10-19
      • 2019-02-09
      • 2018-09-22
      • 2016-07-06
      相关资源
      最近更新 更多