Given these 2 Sequelize models:
export class Users extends Model<Users> {
@HasMany(() => UserRoles)
@Column({ primaryKey: true, allowNull: false, unique: true })
UserId: string;
@Column({ allowNull: false, unique: true })
Email: string;
}
export class UserRoles extends Model<UserRoles> {
@ForeignKey(() => Users)
@Column({ primaryKey: true, allowNull: false, unique: true })
UserId: string;
@Column({ allowNull: false })
RoleId: number;
}
When I create a user I will also create a row in UserRoles
with the UserId
being the foreign key and so there is always a One to one match.
When I try to run this I get:
Error: Naming collision between attribute 'UserId' and association 'UserId' on model Users. To remedy this, change either foreignKey or as in your association definition
I imagine that means that the 2 tables have the same column name UserId
and that is causing the problem? If so how can I fix that? give it some kind of alias? or how?
如果你想要一个一对一的模型,即一个用户只能被分配一个角色,反之亦然,那么主键和外键约束使用相同的字段,否则使用不同的一个
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.