'Adding auto incrementing integer with a starting value in Sequelize

After some research I don't seem to be able to find a great approach doing following:

I wan't to add a new column, to an existing table, that should be an auto incrementing integer, starting with the value of 1000.

My migration file is for now simple and obvious:

'use strict';

module.exports = {
  up: (queryInterface, Sequelize) => {
    return Promise.all([
      queryInterface.addColumn(
        'Orders',
        'orderRef',
        {
          autoIncrement: true,
          type: Sequelize.INTEGER
        }
      )
    ])
  },

  down: {...
  }
};

So this adds the column to the table unproblematic. But I don't seem to be able to find a great approach to set the value to start at 1000 and increment from there.

I know I could run a query like ALTER TABLE Orders AUTO_INCREMENT = 1000; but I would very much appreciate to keep it in Sequelize.

I'm using a PostgreSQL database and Sequelize version 4.38.1.

Does anyone know a nice way around this issue?



Solution 1:[1]

It is already present in Sequelize. There is an option called initialAutoIncrement. This sets the initial AUTO_INCREMENT value for the table in MySQL.

module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define(
    'User',
    {
      id: {
        type: DataTypes.BIGINT(20),
        primaryKey: true,
        autoIncrement: true,
      },
      name: {
        type: DataTypes.STRING(500),
        allowNull: false,
      },
    },
    {
      initialAutoIncrement: 1000,
    }
  );

  return User;
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Robinson De La Cruz