sequelize-typescript sync({force: true}) attempting to create constraints before tables

Viewed 370

I am using sequelize-typescript for my project. In order to speed up local development, I have usually used the sync option in my local environment. I have noticed that sequelize-typescript does attempt to create constraints before all tables are created. Is this me doing something wrong or is this a bud in the sequelize?

Some code example:

Group Model:

@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 Model:

@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[];
}

Resulting error:

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
0 Answers
Related