Convert SQL Primary Keys and Auto-Increment to Sequelize
See how SQL PRIMARY KEY, SERIAL, AUTO_INCREMENT, and IDENTITY columns are converted to Sequelize primaryKey and autoIncrement options in model definitions.
Detailed Explanation
Primary Keys and Auto-Increment in Sequelize
Primary keys are the foundation of every database model. The converter handles all common SQL patterns for defining primary keys and auto-incrementing columns.
SERIAL (PostgreSQL)
PostgreSQL's SERIAL is shorthand for an auto-incrementing integer with a sequence:
id SERIAL PRIMARY KEY
-- becomes -->
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false }
BIGSERIAL maps to DataTypes.BIGINT with the same options.
AUTO_INCREMENT (MySQL)
MySQL's AUTO_INCREMENT keyword maps directly to autoIncrement: true:
id INT AUTO_INCREMENT PRIMARY KEY
-- becomes -->
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false }
IDENTITY (SQL Server)
SQL Server uses IDENTITY(1,1) for auto-incrementing columns:
id INT IDENTITY(1,1) PRIMARY KEY
-- becomes -->
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false }
UUID Primary Keys
For UUID-based primary keys, the SQL type maps to DataTypes.UUID:
id UUID PRIMARY KEY DEFAULT gen_random_uuid()
-- becomes -->
id: { type: DataTypes.UUID, primaryKey: true, defaultValue: DataTypes.UUIDV4, allowNull: false }
Sequelize Behavior
When autoIncrement: true is set, Sequelize:
- Omits the column from INSERT statements (lets the database assign the value)
- Reads the generated value back after insertion
- Sets it on the model instance automatically
In TypeScript mode, auto-increment fields are marked as optional in the CreationAttributes type since they do not need to be provided when creating a record.
Use Case
You are migrating a MySQL database with AUTO_INCREMENT columns to PostgreSQL with SERIAL. The Sequelize model uses the same autoIncrement: true option regardless of the underlying database, making migration seamless.
Try It — SQL to Sequelize Model
Related Topics
Convert a Simple SQL Table to a Sequelize Model
Basic Models
Convert SQL FOREIGN KEY to Sequelize belongsTo Association
Associations
Convert SQL Composite Primary Keys to Sequelize Indexes
Advanced Features
Map SQL Integer Types to Sequelize DataTypes
Basic Models
Convert SQL TIMESTAMP and DATE to Sequelize DATE and DATEONLY
Data Types