Laravel迁移,无法创建外键

huangapple go评论55阅读模式
英文:

Laravel migration, can't create foreign key

问题

以下是要翻译的内容:

Those are the following migration files:

User migration

Schema::create('users', function (Blueprint $table) {
    $table->uuid('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->timestamps();
});

Log migration

Schema::create('log', function (Blueprint $table) {
    $table->uuid('id');
    $table->boolean('status');
    $table->timestamps();
    $table->foreign('id')->references('id')->on('users')->onDelete('cascade');
});

When I run the migrations it shows:

PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table laravel.log (errno: 150 "Foreign key constraint is incorrectly formed")")

I also tried:

Schema::create('log', function (Blueprint $table) {
    $table->uuid('id');
    $table->boolean('status');
    $table->timestamps();
});

Schema::table('log', function($table) {
    $table->foreign('id')
        ->references('id')
        ->on('users')
        ->onDelete('cascade');
});
Schema::create('log', function (Blueprint $table) {
    $table->uuid('id')->primary();
    $table->foreignUuid('foreign_id');
    $table->boolean('status');
    $table->timestamps();
});

Schema::table('log', function($table) {
    $table->foreign('foreign_id')
        ->references('id')
        ->on('users')
        ->onDelete('cascade');
});

But still not working.

-- I have solved this problem

User

Schema::create('users', function (Blueprint $table) {
    $table->uuid('id')->primary();
    $table->string('name');
    $table->string('email')->unique();
    $table->timestamps();
});

log

Schema::create('log', function (Blueprint $table) {
    $table->uuid('id')->primary();
    $table->foreignUuid('user_id');
    $table->boolean('status');
    $table->timestamps();
}
Schema::table('log', function($table) {
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
英文:

Those are the following migration files:

User migration

Schema::create('users', function (Blueprint $table) {
    $table->uuid('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->timestamps();
});

Log migration

Schema::create('log', function (Blueprint $table) {
    $table->uuid('id');
    $table->boolean('statu');
    $table->timestamps();
    $table->foreign('id')->references('id')->on('users')->onDelete('cascade');
});

When I run the migrations it shows:

> PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table laravel.log (errno: 150 "Foreign key constraint is incorrectly formed")")

I also tried:

Schema::create('log', function (Blueprint $table) {
    $table->uuid('id');
    $table->boolean('statu');
    $table->timestamps();
});

Schema::table('log', function($table) {
    $table->foreign('id')
        ->references('id')
        ->on('users')
        ->onDelete('cascade');
});

Schema::create('log', function (Blueprint $table) {
    $table->uuid('id')->primary();
    $table->foreignUuid('foreign_id');
    $table->boolean('statu');
    $table->timestamps();
});

Schema::table('log', function($table) {
    $table->foreign('foreign_id')
        ->references('id')
        ->on('users')
        ->onDelete('cascade');
});

But still not working.

-- I have solved this problem

User

 Schema::create('users', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamps();
        });

log

Schema::create('log', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignUuid('user_id');
            $table->boolean('statu');
            $table->timestamps();
        });
        Schema::table('log', function($table) {
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    
        });

答案1

得分: 2

尝试将以下内容添加到您的用户表中:

$table->uuid('id')->primary();

这里的代码告诉数据库将'id'列设置为主键。

然后,您应该将您的外键命名为“表的名称_id”。对于您来说,应该是:

$table->foreignUuid('foreign_id');

最后,您可以添加:

$table->foreignUuid('user_id')->constrained();

完整的代码如下:

Schema::create('users', function (Blueprint $table) {
    $table->uuid('id')->primary();
    $table->string('name');
    $table->string('email')->unique();
    $table->timestamps();
});

Schema::create('log', function (Blueprint $table) {
    $table->uuid('id');
    $table->boolean('statu');
    $table->timestamps();
    $table->foreignUuid('user_id')->constrained();
});

希望这对您有所帮助。

英文:

Try adding to your users table :

$table->uuid('id')->primary();

Nothing here indicate to you database that id is the primary key.

Then you should call your foreign key as "name_of_the_table"_id.
For you it should be :

$table->foreignUuid('foreign_id');

And finally you can add :

$table->foreignUuid('user_id')->constrained();

The complete code is something like that :

Schema::create('users', function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamps();
    });

 Schema::create('log', function (Blueprint $table) {
        $table->uuid('id');
        $table->boolean('statu');
        $table->timestamps();
        $table->foreignUuid('user_id')->constrained();
    });

答案2

得分: -1

请使用 $table->id(); 而不是 $table->uuid('id');

英文:

Please use $table->id(); instead of $table->uuid('id');

huangapple
  • 本文由 发表于 2023年3月9日 18:13:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定