alter auto increment using knexjs migration (postgres)

3.9k Views Asked by At

I'm using knex.js and migrating one of the column to start from a different number (auto_increment). Is there a better way to alter the auto increment column using Postgres and Knex

const TABLE = 'my_table'
const COL = 'id';

exports.up = function (knex, Promise) {
  return Promise.all([
    knex.schema.withSchema('public').raw(`ALTER SEQUENCE ${TABLE}_${COL}_seq RESTART WITH 100000000000`)
  ]);
};

exports.down = function (knex, Promise) {};

I rather use knex function than .raw

1

There are 1 best solutions below

0
On

Knex is made for multiple databases, and they all handle auto-increment differently.

I'm pretty sure this is the most straightforward way to do it.

You can create the column with createTable().increments(), but I do not believe you can reset it.

https://knexjs.org/#Schema-increments