Skip to content

queryInterface.createTable: "uniqueKeys" option does not work anymore #9041

Closed

Description

@wolframkleis

What are you doing?

In a migration file, I used the following snippet to create a table, and specified the "uniqueKeys" option to create a unique constraint.

sequelize.queryInterface.createTable("MyModel",  {
    id:   {   type: Sequelize.INTEGER, primaryKey: true},
    col1: {   type: Sequelize.INTEGER},
    col2: {   type: Sequelize.INTEGER}
  },
  {  "uniqueKeys": { "col1_col2_unique": { "fields": ["col1","col2"]}}
});

What do you expect to happen?

I wanted Sequelize to create a unique constraint on columns col1 and col2.
Up to 4.33.2 this used to work. The generated SQL was:

CREATE TABLE IF NOT EXISTS "MyModel" 
("id" INTEGER , "col1" INTEGER, "col2" INTEGER, 
UNIQUE ("col1", "col2"), PRIMARY KEY ("id"));

What is actually happening?

With 4.33.3 this is no longer working.
The "uniqueKeys" option is now ignored
Generated SQL no longer has the UNIQUE ("col1", "col2") part

CREATE TABLE IF NOT EXISTS "MyModel" 
("id" INTEGER , "col1" INTEGER, "col2" INTEGER, 
PRIMARY KEY ("id"));

Dialect: postgres
Dialect version: 9.6
Database version: 9.6
Sequelize version: 4.33.3
Tested with latest release: 4.33.3

Activity

  1. added
    type: bug
    on Feb 13, 2018
  2. sushantdhiman commented on Feb 13, 2018

    @sushantdhiman
    Contributor

    For now you can fix this by setting customIndex: true

    "uniqueKeys": { "col1_col2_unique": { "fields": ["col1","col2"], customIndex: true } }
  3. SimonSchick commented on Mar 13, 2018

    @SimonSchick
    Contributor

    I just traced this down as well: #9041

    Can you please mark this as a breaking change?
    This caused us quite some headache today.

  4. sushantdhiman commented on Mar 13, 2018

    @sushantdhiman
    Contributor

    Yes, I am aware this was an unintentional breaking change. I was simply not aware that uniqueKeys was still used.

    I will try to come up with a solution to make this non breaking

  5. SimonSchick commented on Mar 13, 2018

    @SimonSchick
    Contributor

    @sushantdhiman Are you suggesting uniqueKeys shouldn't be used? Is there a better way to define unique keys, maybe via attributes in createTable?

  6. sushantdhiman commented on Mar 13, 2018

    @sushantdhiman
    Contributor

    It seems there is no API other than uniqueKeys although indexes should be available on createTable, making this a breaking change.

    But I can't revert that fix. So as I said I will try to come up with a strategy to keep that fix while making it a non breaking

  7. SimonSchick commented on Mar 13, 2018

    @SimonSchick
    Contributor

    I came up with:

      // Begin patch https://github.com/sequelize/sequelize/issues/9041
      const proto = seq.getQueryInterface().constructor.prototype;
      const old = proto.createTable;
      proto.createTable = function(
        tableName: string | { schema?: string; tableName?: string },
        attributes: ModelAttributes,
        options?: QueryInterfaceCreateTableOptions,
      ): Bluebird<void> {
        if (options && options.uniqueKeys) {
          for (const [, value] of Object.entries(options.uniqueKeys)) {
            if (value.fields.length > 1) {
              (<any> value).customIndex = true;
            }
          }
        }
        return old.call(this, tableName, attributes, options);
      };
      // End patch

    Do you think this will have any side unwanted side effects?

  8. sushantdhiman commented on Mar 13, 2018

    @sushantdhiman
    Contributor

    @SimonSchick Looks good, I see no potential issues with this patch

  9. SimonSchick commented on Apr 29, 2018

    @SimonSchick
    Contributor

    @sushantdhiman have you had any time to apply my patch to the code base?

  10. SimonSchick commented on Sep 17, 2018

    @SimonSchick
    Contributor

    poke, this is still kind of un-intuitive

  11. added this to the v5 milestone on Sep 17, 2018
  12. TannerS commented on Jul 26, 2024

    @TannerS

    customIndex

    sorry for bringing up an old thread, what does customIndex actually mean/do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions