【问题标题】:Laravel Database Migration foreign key error while using "php artisan migrate" command使用“php artisan migrate”命令时 Laravel 数据库迁移外键错误
【发布时间】:2021-08-21 16:56:53
【问题描述】:

Error: SQLSTATE[HY000]: General error: 1005 Can't create table epicit_cms.ep_users (errno: 150 "外键约束格式不正确") (SQL: alter table ep_users add constraint @ 987654324@外键(client_id)在删除级联时引用ep_clients(id)

有人能解释一下应该怎么做吗?

我想要实现的是。如果我想删除一个客户端,它也应该删除具有相同客户端 ID 的用户,但我无法做到这一点,因为我无法迁移我的外键

迁移:

用户表

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->string('name');
        $table->string('insertion')->nullable();
        $table->string('secondname');
        $table->string('email')->unique();
        $table->string('password');
        $table->integer('client_id');  
        $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');            
        $table->boolean('admin')->default(0);
        $table->rememberToken();
        $table->timestamps();
    });

    
}

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

客户表

public function up()
{
    Schema::create('clients', function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->string('company_name');
        $table->string('postal_code');
        $table->string('street');
        $table->timestamps();
    });
}

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

型号:

用户关系

public function Client()
{
    return $this->belongsTo(Client::class );
}

客户关系

public function user()
{
    return $this->hasMany(User::class);
}

【问题讨论】:

  • 我怀疑这是由于在clients 上使用$table->uuid('id')->primary();,然后尝试在users 上使用$table->integer('client_id');。列类型必须相同才能使外键起作用。
  • 你的表客户端还存在吗?通常首先创建用户(客户端应该链接到用户)
  • @TimLewis 我查看了数据库结构“$table->uuid('id')->primary();”在“clients”上它的 cha(36) 所以我从“$表->整数('client_id');"在 "users" 上到 " $table->char('client_id', 36)->default(0); " 仍然没有进展
  • @Dri372 没有办法在用户之前更改迁移订单客户
  • “clients”表迁移会创建一个名为“fields”的表。 (并且会删除一个名为“form_fields”的表)。如果要在创建“用户”表之前创建表,请将迁移重命名为用户迁移日期之前的日期

标签: laravel


【解决方案1】:

像这样改变你的迁移

   public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('name');
            $table->string('insertion')->nullable();
            $table->string('secondname');
            $table->string('email')->unique();
            $table->string('password');
            $table->foreignId('client_id')
                ->constrained()
                ->onDelete('cascade');            
            $table->boolean('admin')->default(0);
            $table->rememberToken();
            $table->timestamps();
        });
    
        
    }
    
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }

【讨论】:

    【解决方案2】:

    您应该确保您的Clients table 迁移在users table 迁移之前运行。

    而且您的foreign_key 字段和您的id 应该属于同一类型,例如,如果clients.id 是bigIncrements,那么您的user table 中的foreign_key 也应该是bigInteger。

    【讨论】:

      【解决方案3】:

      好的,谢谢你们展示了不止一种方法。我使用$table->foreignId('client_id')->constrained()->onDelete('cascade'); 修复了我的代码,就像@sherifcoder 所做的那样,我将客户端表格从$table->uuid('id')->primary(); 更改为$table->id('id');。我对UUID 进行了更多研究,它似乎是UUID 它没有并不总是与FKS 一起工作。再次感谢您的时间

      【讨论】:

        猜你喜欢
        • 2017-09-27
        • 1970-01-01
        • 2016-11-25
        • 2016-09-23
        • 2020-06-20
        • 2018-11-01
        • 2020-10-23
        • 2019-10-29
        • 2018-08-04
        相关资源
        最近更新 更多