Sequelize-typescript: composite unique constraint

Created on 21 Aug 2017  路  5Comments  路  Source: RobinBuschmann/sequelize-typescript

Is it possible to define composite unique constraints like this ?

const ItemTag = sequelize.define('item_tag', {
  id : {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  tag_id: {
    type: DataTypes.INTEGER,
    unique: 'item_tag_taggable'
  },
  taggable: {
    type: DataTypes.STRING,
    unique: 'item_tag_taggable'
  },
  taggable_id: {
    type: DataTypes.INTEGER,
    unique: 'item_tag_taggable',
    references: null
  }
});

Most helpful comment

With the Column annotation:

@Column({
  unique: 'item_tag_taggable',
  ...,
})

All 5 comments

With the Column annotation:

@Column({
  unique: 'item_tag_taggable',
  ...,
})

tnx

Is it possible to define two composite unique constraints where there is an intersection between columns?
I mean:

 await queryInterface.addConstraint('a_b', ['a_id', 'b_id'], {
      type: 'unique',
      name: 'a_b_unique_constraint',
    });
    await queryInterface.addConstraint('a_c', ['a_id', 'c_id'], {
      type: 'unique',
      name: 'a_c_unique_constraint',
    });

As you see there is a column a_id which is a part of two composite unique indexes.
How can I define this property in model description?
Smth like ?

@Column({
    unique: [
      'a_b_unique_constraint',
      'a_c_unique_constraint'
    ]
    type: DataType.INTEGER,
  })

Hey @NickTaylor98, I'm not sure. What does the sequelize docs say?

@NickTaylor98 were you able to solve 2 unique constraints issue?

Was this page helpful?
0 / 5 - 0 ratings