我需要将外键添加到引用健身房 ID 的用户表中,
什么是正确的语法?
export class User extends Model<User> {
// user properties:
@PrimaryKey
@AutoIncrement
@Column(DataType.INTEGER)
public id: number;
@AllowNull(false)
@Column(DataType.STRING)
public first_name: string;
// here i need to reference the gym_id to the id from the gym table:
@AllowNull(false)
@Column(DataType.INTEGER)
// @ForeignKey(Gym.id)
public gym_id: number;
}
最佳答案
这取决于什么样的关联:
请查看此文档:https://www.npmjs.com/package/sequelize-typescript#model-association
如果是一对多,那么就像这样:
@ForeignKey(() => Gym)
@Column
gym_id: number
@BelongsTo(() => Gym)
gym: Gym
关于node.js - 如何添加外键来 Sequelize 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66833228/