我希望将来有多个Address用于Person、Location或Place中的任何一个,也可能用于其他实体类型。我希望在我的Address模型上有一个字段,它指向地址相关的任何东西的id,而不必有personId和locationId和placeId等。在每个Address上,而他们只会使用其中的一个。
我的表结构如下所示:
addresses.entityId => people.id | locations.id | places.id
如何在sequelize-typescript中使用@ForeignKey对此进行建模
我的模型是这样的:
@Table
class Address extends Model<Address> {
@Column({
type: DataType.UUID,
allowNull: false,
primaryKey: true,
unique: true,
defaultValue: DataType.UUIDV4
})
id: string;
@ForeignKey // ... ??
entityId: string;
}
@Table
class Person extends Model<Person> {
@Column({
type: DataType.UUID,
allowNull: false,
primaryKey: true,
unique: true,
defaultValue: DataType.UUIDV4
})
id: string;
}
@Table
class Location extends Model<Location> {
@Column({
type: DataType.UUID,
allowNull: false,
primaryKey: true,
unique: true,
defaultValue: DataType.UUIDV4
})
id: string;
}
@Table
class Place extends Model<Place> {
@Column({
type: DataType.UUID,
allowNull: false,
primaryKey: true,
unique: true,
defaultValue: DataType.UUIDV4
})
id: string;
}发布于 2018-11-27 22:14:49
在这种Sequelize Typescript情况下,可以多次指定自定义装饰器,所以如果您希望将一个列引用到多个模型,那么可以多次指定@ForeignKey,就像我给出的这个代码片段一样。你可以这样做:
@Table
class Address extends Model<Address> {
@Column({
type: DataType.UUID,
allowNull: false,
primaryKey: true,
unique: true,
defaultValue: DataType.UUIDV4
})
id: string;
@ForeignKey(() => Person)
@ForeignKey(() => Location)
@ForeignKey(() => Place)
@Column({})
public entityId: string;
} https://stackoverflow.com/questions/52814646
复制相似问题