question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Knex.Schema: How to create composite unique key?

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

53reactions
tgriessercommented, Aug 12, 2013

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.

29reactions
amitava82commented, Aug 12, 2013

Works, thanks! Preparing to switch from Sequelize.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I add a Composite primary key with Knex.js?
If you need to create a composite primary key, call it on a table with an array of column names instead. Constraint name...
Read more >
Schema Builder | Knex.js
If you need to create a composite index, pass an array of column to columns . Deferrable unique constraint are supported on Postgres...
Read more >
How to create Composite Primary Key? - Wappler Community
You would need to create a migration file, undo the changes, edit it and reapply the migration.
Read more >
Composite keys | Objection.js - GitHub Pages
Composite (compound) keys are fully supported. Just give an array of columns where you would normally give a single column name. Composite primary...
Read more >
Knex(postres) - Is there a way to have a column unique only ...
That would be a unique constraint defined on both columns: ALTER TABLE tab ADD UNIQUE (orgid, name);. That will exclude only rows where...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found