无法在 Laravel 迁移中创建外键

时间:2021-03-04 14:16:17

标签: php laravel jetstream

我正在尝试使用 user_role 表在用户表中创建外键关系。

用户表迁移

user_role 列是外键列

[2021-03-04T13:30:51.367Z] 275177 , 1.FFC-Frankfurt , ´e???st? ?f ?f ?e? ´fra?kf?rt

user_role 表

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('user_role')->nullable();
            $table->foreign('user_role')->references('role')->on("user_role")->onDelete('cascade');
            $table->rememberToken();
            $table->foreignId('current_team_id')->nullable();
            $table->text('profile_photo_path')->nullable();
            $table->timestamps();
        });
    }

迁移未应用以下错误正在生成

public function up()
    {
        Schema::create("user_role",function(Blueprint $table){
            $table->increments('id');
            $table->string("role")->nullable();
        });
    }

我做错的外键有什么问题。

2 个答案:

答案 0 :(得分:0)

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->unsignedBigInteger('user_role')->nullable();
            $table->foreign('user_role')->references('id')->on("user_role")->onDelete('cascade');
            $table->rememberToken();
            $table->foreignId('current_team_id')->nullable();
            $table->text('profile_photo_path')->nullable();
            $table->timestamps();
        });
    }

在创建用户迁移之前创建 user_role 迁移并在 id 上使用字段 user_role

答案 1 :(得分:0)

您的角色必须是独一无二的。

$table->string("role")->unique();

这是用户迁移的代码

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->foreignId('current_team_id')->nullable();
            $table->text('profile_photo_path')->nullable();
            $table->timestamps();
        });
    }

然后使用此代码创建“user_role”迁移。

public function up()
    {
        Schema::create("user_role",function(Blueprint $table){
            $table->increments('id');
            $table->string("role")->unique();
        });
    }

然后创建另一个迁移,如

php artisan make:migration add_user_role_in_users_table

在其中添加此代码

public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('user_role')->nullable();
            $table->foreign('user_role')->references('role')->on("user_role")->onDelete('cascade');
        });
    }

然后运行

php artisan migrate
相关问题