Sequelize is one of the oldest Node.js ORMs with broad multi-dialect support. It does not ship a DataTypes.UUIDV7 shorthand, but DataTypes.UUID with a beforeCreate hook is a clean equivalent. See UUID v4 vs v7 for the database performance rationale.
Model definition with UUID v7
// models/order.ts
import { DataTypes, Model, Sequelize, literal } from 'sequelize';
import { v7 as uuidv7 } from 'uuid';
export class Order extends Model {
declare id: string;
declare customerId: string;
declare status: string;
}
export function initOrder(sequelize: Sequelize) {
Order.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
allowNull: false,
},
customerId: {
type: DataTypes.UUID,
allowNull: false,
field: 'customer_id',
},
status: {
type: DataTypes.STRING(32),
allowNull: false,
},
},
{ sequelize, tableName: 'orders', underscored: true }
);
// Assign UUID v7 before every INSERT
Order.beforeCreate(order => {
if (!order.id) order.id = uuidv7();
});
}
The beforeCreate hook fires before every INSERT in any Sequelize dialect — the if (!order.id) guard preserves explicitly set IDs (useful in tests and upserts). See UUID Testing Strategies.
PostgreSQL 18 — server-side default
id: {
type: DataTypes.UUID,
primaryKey: true,
allowNull: false,
defaultValue: literal('uuidv7()'), // PostgreSQL 18+ — DB generates UUID v7
},
With defaultValue: literal('uuidv7()'), Sequelize emits DEFAULT uuidv7() in the CREATE TABLE DDL. Raw SQL inserts and Sequelize both get UUID v7 without an application-layer hook. Requires PostgreSQL 18.
Migration file
// migrations/20250101000000-create-orders.ts
import { QueryInterface, DataTypes } from 'sequelize';
export async function up(queryInterface: QueryInterface): Promise<void> {
await queryInterface.createTable('orders', {
id: {
type: DataTypes.UUID,
primaryKey: true,
allowNull: false,
},
customer_id: {
type: DataTypes.UUID,
allowNull: false,
},
status: {
type: DataTypes.STRING(32),
allowNull: false,
},
created_at: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
updated_at: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
});
}
export async function down(queryInterface: QueryInterface): Promise<void> {
await queryInterface.dropTable('orders');
}
No autoIncrement or SERIAL is used — Sequelize stores the application-provided UUID v7 as the primary key. See UUID in Databases for the B-tree storage detail.
Querying
import { Op } from 'sequelize';
// Find by primary key
const order = await Order.findByPk('018fbe3a-4c5d-7b12-8abc-0123456789ab');
// Time-range via UUID v7 lower bound
const lowerBound = buildUuidV7Bound(Date.now() - 3_600_000);
const recent = await Order.findAll({
where: { id: { [Op.gte]: lowerBound } },
order: [['id', 'ASC']],
});
[Op.gte] on the UUID column uses the primary key index — no secondary timestamp index needed for time-range pagination. See UUID v7 for Cursor Pagination.
Multi-dialect support
Sequelize’s DataTypes.UUID maps to:
- PostgreSQL →
uuid(16-byte native type) - MySQL / MariaDB →
char(36)(hyphenated string) - SQLite →
text(string) - SQL Server →
uniqueidentifier
UUID v7 strings are valid for all four — the beforeCreate hook is dialect-agnostic.
Further reading
- UUID v7 ORM Support in 2026
- UUID in JavaScript — full guide
- UUID in Databases — storage and indexing
- UUID v4 vs v7 — performance comparison
- UUID v7 for Cursor Pagination
- Zero-Downtime UUID Migration
External references
Frequently asked questions
How do I use UUID v7 as a primary key in Sequelize?
Define the primary key as DataTypes.UUID with no autoIncrement, then add a beforeCreate hook: Order.beforeCreate(order => { order.id = uuidv7(); });. This works for all Sequelize dialects (PostgreSQL, MySQL, SQLite, SQL Server). For PostgreSQL 18, use defaultValue: literal("uuidv7()") to delegate generation to the database. See UUID v7 ORM Support in 2026.
Does Sequelize's DataTypes.UUIDV4 generate UUID v4 or v7?
DataTypes.UUIDV4 always generates UUID v4 — it calls Node.js's crypto.randomUUID() internally. There is no DataTypes.UUIDV7 as of Sequelize 6. Use DataTypes.UUID (generic UUID storage) combined with a beforeCreate hook calling uuidv7() from the uuid package.