使用Laravel迁移将新的UUID外键添加到现有表中

发布于 2025-02-11 07:55:52 字数 1173 浏览 1 评论 0原文

我想在我的用户表中添加新列的,所以这是代码

Schema::table('users', function (Blueprint $table) {
    $table->uuid('uuid')->nullable()->unique();
});

,然后我想在我的>技能表中使用外键以及在此技能表已经从另一个表中具有外键,但是它总是显示错误外键约束是错误形成的

Schema::table('skills', function (Blueprint $table) {
   $table->foreignUuid('user_id')->nullable();
   $table->foreign('user_id')
         ->references('uuid')
         ->on('users')
         ->onDelete('cascade');
});

是否适合我不能添加新的外键,因为我还没有下降和重新列出现有的外键?为什么我要这是因为当我在培训中添加UUID外键表格上完全没有外键的表中,这就是代码

Schema::table('trainings', function (Blueprint $table) {
   $table->foreignUuid('user_id')->nullable();
   $table->foreign('user_id')->references('uuid')->on('users');
});

是错误

sqlstate [hy000]:常规错误:1005无法创建表db_masteremployee技能>技能(errno:150“外键约束不正确地形成”)(sql:sql:sql: Alter Table 技能添加约束Skills_user_id_foreign外键(user_id)参考用户uuid uuid > )删除级联)

I want to add new column in my users table which is uuid so here is the code

Schema::table('users', function (Blueprint $table) {
    $table->uuid('uuid')->nullable()->unique();
});

and then i want to make a relation using foreign key in my skills table, and in this skills table already has foreign key from another table but it always showing error Foreign key constraint is incorrectly formed

Schema::table('skills', function (Blueprint $table) {
   $table->foreignUuid('user_id')->nullable();
   $table->foreign('user_id')
         ->references('uuid')
         ->on('users')
         ->onDelete('cascade');
});

is it posible i cant add new foreign key just because i havent drop and redeclare the existing foreign key ? why i ask that is because when i add uuid foreign key in my trainings table that has no foreign key at all is success here is the code

Schema::table('trainings', function (Blueprint $table) {
   $table->foreignUuid('user_id')->nullable();
   $table->foreign('user_id')->references('uuid')->on('users');
});

here is the error

SQLSTATE[HY000]: General error: 1005 Can't create table db_masteremployee.skills (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table skills add constraint skills_user_id_foreign foreign key (user_id) references users (uuid) on delete cascade)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自在安然 2025-02-18 07:55:52

我认为这是因为uuid不是主要

Schema::table('users', function (Blueprint $table) {
     $table->dropPrimary('id');
     $table->uuid('uuid')->nullable()->unique();
     $table->primary(['id','uuid']);
});

键属于模型中的

    Schema::table('users', function (Blueprint $table) {
        $table->uuid('uuid')->nullable();
    });

    Schema::table('skills', function (Blueprint $table) {
        $table->uuid('user_id')->nullable();
    });

关系代码() / hasmany()

 public function skills(): Hasmany
{
    return $this->hasmany(Skills::class, 'user_id', 'uuid');
}

i think its because the uuid is not a primary key so i decided to use this instead

Schema::table('users', function (Blueprint $table) {
     $table->dropPrimary('id');
     $table->uuid('uuid')->nullable()->unique();
     $table->primary(['id','uuid']);
});

and if you dont want to drop the existing primary key, you can just make a simple column and make your self a relation BelongsTo() / HasMany() in the model

    Schema::table('users', function (Blueprint $table) {
        $table->uuid('uuid')->nullable();
    });

    Schema::table('skills', function (Blueprint $table) {
        $table->uuid('user_id')->nullable();
    });

the relation code

 public function skills(): Hasmany
{
    return $this->hasmany(Skills::class, 'user_id', 'uuid');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文