使用Laravel迁移将新的UUID外键添加到现有表中
我想在我的用户
表中添加新列的,所以这是代码
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 tableskills
add constraintskills_user_id_foreign
foreign key (user_id
) referencesusers
(uuid
) on delete cascade)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是因为
uuid
不是主要键属于模型中的
关系代码() / hasmany()
i think its because the
uuid
is not a primary key so i decided to use this insteadand 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
the relation code