Map SQL Integer Types to Sequelize DataTypes
Learn how SQL integer types including INT, BIGINT, SMALLINT, TINYINT, and MEDIUMINT map to their Sequelize DataTypes equivalents with size considerations.
Detailed Explanation
SQL Integer Types and Sequelize DataTypes
SQL databases support multiple integer sizes for storage optimization. Sequelize mirrors these with dedicated DataTypes constants, and the converter maps each one precisely.
Standard Mapping Table
| SQL Type | Sequelize DataType | Storage | Range |
|---|---|---|---|
TINYINT |
DataTypes.TINYINT |
1 byte | -128 to 127 |
SMALLINT |
DataTypes.SMALLINT |
2 bytes | -32,768 to 32,767 |
MEDIUMINT |
DataTypes.MEDIUMINT |
3 bytes | -8M to 8M |
INT / INTEGER |
DataTypes.INTEGER |
4 bytes | -2B to 2B |
BIGINT |
DataTypes.BIGINT |
8 bytes | Very large |
Example Conversion
CREATE TABLE metrics (
id BIGINT PRIMARY KEY,
status_code SMALLINT NOT NULL,
retry_count TINYINT DEFAULT 0,
request_count INTEGER NOT NULL,
byte_size BIGINT
);
Metric.init({
id: { type: DataTypes.BIGINT, primaryKey: true, allowNull: false },
status_code: { type: DataTypes.SMALLINT, allowNull: false },
retry_count: { type: DataTypes.TINYINT, allowNull: true, defaultValue: 0 },
request_count: { type: DataTypes.INTEGER, allowNull: false },
byte_size: { type: DataTypes.BIGINT, allowNull: true },
}, { ... });
TINYINT(1) Boolean Pattern
MySQL uses TINYINT(1) as a de facto boolean type. The converter detects this specific pattern and maps it to DataTypes.BOOLEAN instead of DataTypes.TINYINT:
is_verified TINYINT(1) DEFAULT 0
-- becomes -->
is_verified: { type: DataTypes.BOOLEAN, defaultValue: false }
PostgreSQL Aliases
PostgreSQL provides INT2 (SMALLINT), INT4 (INTEGER), and INT8 (BIGINT) aliases. The converter recognizes all of these and maps them to the appropriate Sequelize DataType. SERIAL and BIGSERIAL are treated as auto-incrementing INTEGER and BIGINT respectively.
Use Case
You are designing an analytics system with tables that store counters, byte sizes, and status codes. Choosing the correct integer size in Sequelize affects both storage efficiency and the JavaScript number types returned by queries.
Try It — SQL to Sequelize Model
Related Topics
Convert a Simple SQL Table to a Sequelize Model
Basic Models
Map SQL VARCHAR and TEXT to Sequelize STRING and TEXT
Basic Models
Convert SQL Primary Keys and Auto-Increment to Sequelize
Associations
Convert SQL TIMESTAMP and DATE to Sequelize DATE and DATEONLY
Data Types
Convert SQL DEFAULT Values to Sequelize defaultValue
Advanced Features