Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knex.js Schema: Multicolumn Index

Tags:

Is there a way to specify a multicolumn index in the Knex.js schema? Or must one drop to raw and do an alter table?

like image 965
Daniel Avatar asked Mar 12 '14 20:03

Daniel


People also ask

How can set primary key in KNEX?

As per Knex's documentation here: primary — column. primary([constraintName]); table. primary(columns, [constraintName]) When called on a single column it will set that column as the primary key for a table.

Is KNEX js an ORM?

Sequelize is an ORM that includes some query builder stuff; Knex is just a query builder, not an ORM.

What does KNEX return?

What data type is the output? Knex returns an array for all queries, even if there is only one row returned. The name of the user can be accessed from user[0] .

What is KNEX node js?

Knex.js (pronounced /kəˈnɛks/) is a "batteries included" SQL query builder for PostgreSQL, CockroachDB, MSSQL, MySQL, MariaDB, SQLite3, Better-SQLite3, Oracle, and Amazon Redshift designed to be flexible, portable, and fun to use.


1 Answers

Figured this out. You can use the .index chainable on the table directly and pass an array for the index fields and a name for the index.

knex.schema.createTable(function(table) {
  table.bigInteger('_id').unsigned().primary();
  table.string('fieldA');
  table.string('fieldB');
  table.index(['fieldA','fieldB'], 'index_name');
});
like image 153
Daniel Avatar answered Oct 27 '22 09:10

Daniel