Laravel 错误:SQLSTATE[HY000]:一般错误:1005 无法创建表 `distrisalsas`.`product_details` (错误号:150“外键约束是 inc

问题描述 投票:0回答:1

我有一个问题,我正在尝试使用 laravel 11 中的迁移在 mySql 中创建我的表:产品、品牌、参考和产品详细信息,但是当我运行 php artisan migrate 命令时,我收到以下错误:

    2024_05_16_010921_create_product_details_table  45.49ms FAIL

    Illuminate\Database\QueryException 

    SQLSTATE[HY000]: General error: 1005 Can't create table `distrisalsas`.`product_details` 
    (errno: 150 "Foreign key constraint is incorrectly formed") (Connection: mysql, SQL: 
    alter table `product_details` add constraint `product_details_product_id_foreign` foreign 
    key (`product_id`) references `products` (`id`) on delete set null on update cascade)      

    at vendor\laravel\framework\src\Illuminate\Database\Connection.php:813
    809▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    810▕                 );
    811▕             }
    812▕
    ➜ 813▕             throw new QueryException(     
    814▕                 $this->getName(), $query, 
    $this->prepareBindings($bindings), $e
    815▕             );
    816▕         }
    817▕     }

    1   vendor\laravel\framework\src\Illuminate\Database\Connection.php:571
    PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table 
   `distrisalsas`.`product_details` (errno: 150 "Foreign key constraint is incorrectly 
    formed")")

   2   vendor\laravel\framework\src\Illuminate\Database\Connection.php:571
   PDOStatement::execute()

这是我的代码:

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('product');
            $table->string('description');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('products');
    }
};
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('brands', function (Blueprint $table) {
            $table->id();
            $table->string('brand');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('brands');
    }
};
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('references', function (Blueprint $table) {
            $table->id();
            $table->string('reference');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('references');
    }
};

y

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('product_details', function (Blueprint $table) {
            $table->id();            
            $table->foreignId('product_id')
            ->constrained()
            ->cascadeOnUpdate()
            ->nullOnDelete();
            $table->foreignId('brand_id')
            ->nullable()
            ->constrained()
            ->cascadeOnUpdate()
                ->nullOnDelete();
            $table->foreignId('reference_id')
                ->nullable()
                ->constrained()
                ->cascadeOnUpdate()
                ->nullOnDelete();       
            $table->decimal('price')
                ->nullable();
            $table->timestamps();
            });
    }


    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('product_details');
    }
};

我已经检查了数据库中的品牌、产品、参考表,ID 为 BIGINT(20),属性为 UNSIGNED

我感谢任何帮助

mysql migration laravel-11
1个回答
0
投票

您忘记在

->nullable()
 之后添加 
$table->foreignId('product_id')

这是

product_details
表的正确代码:

return new class extends Migration {
/**
 * Run the migrations.
 */
public function up(): void
{
    Schema::create('product_details', function (Blueprint $table) {
        $table->id();
        $table->foreignId('product_id')
            ->nullable()
            ->constrained()
            ->cascadeOnUpdate()
            ->nullOnDelete();
        $table->foreignId('brand_id')
            ->nullable()
            ->constrained()
            ->cascadeOnUpdate()
            ->nullOnDelete();
        $table->foreignId('reference_id')
            ->nullable()
            ->constrained()
            ->cascadeOnUpdate()
            ->nullOnDelete();
        $table->decimal('price')
            ->nullable();
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 */
public function down(): void
{
    Schema::dropIfExists('product_details');
}

};

© www.soinside.com 2019 - 2024. All rights reserved.