我正在为项目使用sequelize-typescript。为了加速本地开发,我通常在本地环境中使用sync选项。我注意到sequelize-typescript确实在创建所有表之前尝试创建约束。这是我做错了吗,还是续集中的萌芽?
一些代码示例:
组模型:
@Table
export default class Group extends Model<Group> {
@Column({
type: DataType.UUID,
defaultValue: DataType.UUIDV4,
primaryKey: true,
})
id: string;
@Column(DataType.STRING())
name: string;
// Associations
@HasMany(() => Transaction, { onDelete: 'SET NULL' })
transactions: Transaction[];
@ForeignKey(() => GroupCategory)
@Column
groupCategoryId: string;
@BelongsTo(() => GroupCategory, { onDelete: 'SET NULL' })
groupCategory: GroupCategory;
@BelongsToMany(() => User, () => UserGroup, { onDelete: 'SET NULL' })
users: User[];
}
GroupCategory模型:
@Table
export default class GroupCategory extends Model<GroupCategory> {
@PrimaryKey
@Column({
type: DataType.UUID,
defaultValue: DataType.UUIDV4,
})
id: string;
@Column(DataType.STRING)
name: string;
@Column(DataType.STRING(1500))
pictureSRC: string;
@Column(DataType.STRING)
backgrundColor: string;
@Column(DataType.STRING(3))
icon: string;
@HasMany(() => Group, { onDelete: 'SET NULL' })
groups: Group[];
}
产生的错误:
Executing (default): CREATE TABLE IF NOT EXISTS "Groups" ("id" UUID , "name" VARCHAR(255), "groupCategoryId" VARCHAR(255) REFERENCES "GroupCategories" ("id") ON DELETE SET NULL ON UPDATE CASCADE, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL, PRIMARY KEY ("id"));
(node:82264) UnhandledPromiseRejectionWarning: SequelizeDatabaseError: foreign key constraint "Groups_groupCategoryId_fkey" cannot be implemented