Knex: Knex.Schema: How to create composite unique key?

Created on 12 Aug 2013  ·  12Comments  ·  Source: knex/knex

For example,

ALTER TABLE users ADD UNIQUE KEY(LoginID, Email);
ALTER TABLE users ADD UNIQUE KEY(Email);
ALTER TABLE users ADD UNIQUE KEY(LoginID);

How do I do it with Knex.Schema.createTable

question

Most helpful comment

I believe you should be able to do this with:

Knex.Schema.createTable('tableName', function(table) {
  table.unique(['LoginID', 'Email']);
  table.unique('LoginID');
  table.unique('Email');
});

Let me know if it doesn't seem to be working.

All 12 comments

I believe you should be able to do this with:

Knex.Schema.createTable('tableName', function(table) {
  table.unique(['LoginID', 'Email']);
  table.unique('LoginID');
  table.unique('Email');
});

Let me know if it doesn't seem to be working.

Works, thanks! Preparing to switch from Sequelize.

i try to run a migration

knex.schema.createTable('users', function (table) {
        table.increments('id').primary();
        table.string('network', 3).notNullable();
        table.string('uid', 64).notNullable();
        table.unique(['network', 'uid']);
        table.timestamps();
}).then(function () {
        console.log('Users Table is Created!');
});

But i won't get a unique index

CREATE TABLE `users` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `network` varchar(3) NOT NULL,
  `uid` varchar(64) NOT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

What can be wrong?

@theo-ptz The API changed a while back. Per the docs, unique is now a chainable method that you'd use the same way you're using primary and notNullable.

@bendrucker, thanks.

But something doesn't work.
I try to run this code

knex.schema.createTable('users', function (table) {
    table.increments('id').primary();
    table.string('network', 3).notNullable();
    table.string('uid', 64).unique();
    table.timestamps();
})

I saw this in debug
--> ComQueryPacket { command: 3, sql: 'create tableusers(idint(11) unsigned not null auto_increment primary key,networkvarchar(3) not null,uidvarchar(64),created_atdatetime,updated_atdatetime) default character set utf8' }

I didn't get an index.

 CREATE TABLE `users` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `network` varchar(3) NOT NULL,
  `uid` varchar(64) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

What can be wrong?

Is there anyway to create a composite unique index in migration?

See the integration tests for an example for a composite unique constraint:

https://github.com/tgriesser/knex/blob/master/test/integration/schema/index.js#L92

@bendrucker when you link to a source file please use a tag/commit instead of master:
Your link that does not work anymore: https://github.com/tgriesser/knex/blob/master/test/integration/schema/index.js#L92
VS what you meant: https://github.com/tgriesser/knex/blob/0.5.11/test/integration/schema/index.js#L92
And using latest version (0.8.6): https://github.com/tgriesser/knex/blob/0.8.6/test/integration/schema/index.js#L157

Protip: if you press the "y" key while browsing on github it switches to the current sha url for the current file you're viewing. Super convenient, I wish github would just automatically do that when you paste in a gh url in a comment

Has this feature been deprecated since it's not in the documentation?

@RichardSimko According to the test suite the table.unique([...]) syntax for compound keys is still supported, so it looks like a bug with the documentation.

Yeah, I realized that it still worked, I was just worried that it's about to be removed due to it not being in the docs.

So what's the difference between table.unique([...]) and table.primary([...]) ?

As per the docs it seems using primary([...]) is the proper way of doing composite primary keys:

When called on a single column it will set that column as the primary key for a table. To create a compound primary key, pass an array of column names: table.primary(['column1', 'column2']). Constraint name defaults to tablename_pkey unless constraintName is specified.

http://knexjs.org/#Schema-primary

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marianomerlo picture marianomerlo  ·  3Comments

lanceschi picture lanceschi  ·  3Comments

sandrocsimas picture sandrocsimas  ·  3Comments

rarkins picture rarkins  ·  3Comments

mishitpatel picture mishitpatel  ·  3Comments