Search code examples
knex.js

How can I find the primary key of a table using knex.js?


How can I find the primary key of a table using knex.js?

Using columnInfo, I can obtain the column schema:

knex('my_table').columnInfo().then(info => ...)

But this doesn't appear to identify primary keys. Is there any way to find primary key and other constraint information?


Solution

  • You need to use suitable knex.raw() query for your database and then parse result. Knex (0.13) doesn't have any helper for it.