Convert a Simple SQL Table to a Sequelize Model
Learn how to convert a basic SQL CREATE TABLE statement into a Sequelize Model.init() definition with DataTypes mapping, allowNull, and tableName options.
Detailed Explanation
From CREATE TABLE to Sequelize Model.init()
The most fundamental conversion takes a SQL CREATE TABLE statement and produces a Sequelize Model.init() call. Each column becomes a field definition with a DataTypes constant, and constraints like NOT NULL are mapped to allowNull: false.
Example SQL
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(320) NOT NULL,
age INTEGER,
is_active BOOLEAN NOT NULL DEFAULT true
);
Generated Sequelize Model
User.init({
id: { type: DataTypes.INTEGER, primaryKey: true, allowNull: false },
name: { type: DataTypes.STRING(255), allowNull: false },
email: { type: DataTypes.STRING(320), allowNull: false },
age: { type: DataTypes.INTEGER, allowNull: true },
is_active: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true },
}, {
sequelize,
modelName: 'User',
tableName: 'users',
});
Key Mapping Rules
| SQL element | Sequelize equivalent |
|---|---|
| Table name | tableName option (original) + modelName (PascalCase singular) |
| Column name | Field key in the init() attributes object |
NOT NULL |
allowNull: false |
| Nullable | allowNull: true |
PRIMARY KEY |
primaryKey: true |
DEFAULT |
defaultValue: ... |
Model Class vs define()
Sequelize supports two patterns: the class-based Model.init() approach and the sequelize.define() function. This converter generates the class-based pattern because it works better with TypeScript, supports static methods for associations, and is the recommended approach in Sequelize v6+. The tableName option is always set explicitly to preserve the exact SQL table name, while modelName uses PascalCase singular form by convention.
Use Case
You have an existing SQL database and want to quickly scaffold Sequelize models for a new Node.js API. Instead of manually writing each model, paste the CREATE TABLE statements and get production-ready model definitions instantly.
Try It — SQL to Sequelize Model
Related Topics
Map SQL VARCHAR and TEXT to Sequelize STRING and TEXT
Basic Models
Map SQL Integer Types to Sequelize DataTypes
Basic Models
Convert SQL Primary Keys and Auto-Increment to Sequelize
Associations
Convert SQL DEFAULT Values to Sequelize defaultValue
Advanced Features
Convert SQL TIMESTAMP and DATE to Sequelize DATE and DATEONLY
Data Types