How to use drizzle-seed version 3
To use drizzle-seed version 3, call await seed(db, schema) with no version option (uses latest by default) or explicitly with await seed(db, schema, { version: '3' }). To switch back to version 1, call await seed(db, schema, { version: '1' }).
How to use drizzle-seed version 4
To use drizzle-seed version 4, call await seed(db, schema) with no version option (uses latest by default) or explicitly with await seed(db, schema, { version: '4' }). To switch back to previous versions: await seed(db, schema, { version: '1' }) for version 1 uuid generator, version '2' for v2 generators with v1 uuid generator, or version '3' for v3 generators with v1 uuid generator.
drizzle-seed uuid generator validation example
When using the uuid() generator with Zod validation, the old version generated UUIDs that failed validation. Example: after seeding with the old uuid generator and calling createSelectSchema(schema.uuidTest).parse(res[0]), it would throw an error. Version 4 fixes this by generating valid UUID v4 values that pass Zod validation.
drizzle-seed versioning overview
drizzle-seed uses versioning to manage outputs for static and dynamic data. Versioning ensures determinism: values remain unchanged when using the same seed number. When changes are made to static data sources or dynamic data generation logic, the version is updated, allowing you to choose between sticking with the previous version or using the latest. You can upgrade to the latest drizzle-seed version for new features while maintaining deterministic outputs with a previous version if needed.
drizzle-seed version API versions and npm versions history
drizzle-seed versions are: v1 (npm 0.1.1), v2 (npm 0.2.1) with changed string() and interval({ isUnique: true }) generators, v3 (npm 0.4.0) with hash generating function changes, v4 LTS (npm 1.0.0-beta.8) with uuid generator changes.
How to specify drizzle-seed version
To specify a drizzle-seed version, pass a version option to the seed function: await seed(db, schema, { version: '2' }). You can specify any version number as a string to control which generators are used.
drizzle-seed version 2 interval generator change
In drizzle-seed version 2, the unique interval() generator was changed. The older version could produce intervals like '1 minute 60 seconds' and '2 minutes 0 seconds' as distinct values. However, when '1 minute 60 seconds' is inserted into MySQL, it is automatically converted to '2 minutes 0 seconds'. This caused unique constraint violations. Version 2 fixes this by generating properly normalized intervals.
drizzle-seed version 2 string generator change
In drizzle-seed version 2, the string() generator (both unique and non-unique versions) was changed. The upgrade adds the ability to generate unique strings based on the column length parameter (e.g., varchar(20)). This affects tables with text-like columns that have a maximum length parameter or unique text-like columns.
drizzle-seed version 3 hash function change
In drizzle-seed version 3, the hash generating function was changed. The previous version generated different hashes depending on whether Bun or Node.js was used, and hashes varied across Node.js versions. The new hash generating function generates the same hash regardless of Node.js or Bun version, resulting in deterministic data generation across all runtime versions. All generators output different values compared to version 2, even with the same seed number.
drizzle-seed version 4 uuid generator change
In drizzle-seed version 4 (LTS), the uuid() generator was changed. The old version generated UUID values that failed Zod's v4 UUID validation. Version 4 fixes this by generating UUIDs that pass Zod v4 validation.
UUID generator upgraded to v4 in drizzle-seed
The UUID generator now produces RFC 4122 v4 compliant UUIDs ensuring compatibility with Zod v4 validation. Use { version: '1' } to keep old behavior or { version: '2' | '3' } for newer generators.
drizzle-seed ignore columns in refinements
Skip specific columns during seeding by setting them to false in the columns object. The database default will be used instead. Example: await seed(db, schema).refine((f) => ({ users: { count: 5, columns: { name: f.fullName(), photo: false } } }));
Time-based generators min/max parameters
Time-based generators now accept min and max boundary parameters: funcs.time({ min: '13:12:13', max: '15:12:13' }), funcs.timestamp({ min: '2024-01-01T00:00:00Z', max: '2025-01-01T00:00:00Z' }), funcs.datetime({ min: '2024-01-01', max: '2025-01-01' })
Seeding with partially exposed schema - foreign key error
When seeding a table with a foreign key reference to an unexposed table, if the foreign key column has a not-null constraint, the seeding script will throw an error: 'Column [name] has not null constraint, and you didn't specify a table for foreign key on column [name] in [table] table.' This occurs because the seed function cannot generate values for the foreign key column if the referenced table is not provided to the seed function schema.
Seeding with partially exposed schema - foreign key warning
When seeding a table with a nullable foreign key reference to an unexposed table, the seeding script will show a warning: 'Column [name] in [table] table will be filled with Null values because you specified neither a table for foreign key on column [name] nor a function for [name] column in refinements.' The foreign key column will be populated with null values unless explicitly refined.
Seeding - resolving foreign key constraint errors
There are three ways to resolve a foreign key constraint error during seeding: (1) Remove the not-null constraint from the foreign key column; (2) Expose the referenced table to the seed function schema by adding it to the seed call (e.g., await seed(db, { childTable, parentTable })); (3) Refine the foreign key column generator using the refine method to specify custom values.
Seeding - refining foreign key column generator
Use the refine method on the seed function to customize foreign key column generation. Call .refine((funcs) => ({ tableName: { columns: { columnName: funcs.valuesFromArray({ values: [1, 2] }) } } })) to provide specific values for a foreign key column. This requires the referenced table to already have the specified IDs in the database.
Seeding with refine method - example with foreign key values
Example showing how to refine a foreign key column during seeding:
```ts
import { bloodPressure } from './schema.ts';
async function main() {
const db = drizzle(...);
await seed(db, { bloodPressure }).refine((funcs) => ({
bloodPressure: {
columns: {
userId: funcs.valuesFromArray({ values: [1, 2] })
}
}
}));
}
main();
```
This example seeds the bloodPressure table and specifies that the userId column should be filled with values 1 and 2 from an array.
Drizzle Seed: using 'with' option for one-to-many relationships
The 'with' option in Drizzle Seed is used to seed related tables based on one-to-many relationships. If one user has many posts, you can use 'with' to generate child records. The syntax is: users: { count: 2, with: { posts: 3 } }, which generates 2 users each with 3 posts.
Drizzle Seed: 'with' requires foreign key reference or explicit relation
To use 'with' in seeding, the child table must either have a foreign key reference to the parent table (using .references()), or you must include an explicit one-to-many relation definition in your schema and pass it to the seed function schema parameter. Without either, seeding with 'with' will fail with an error stating the table doesn't have a reference.
Drizzle Seed: 'with' respects one-to-many relationship direction
The 'with' option must follow the direction of the one-to-many relationship defined in the schema. If the relationship is one user has many posts, you must seed users first with posts in the 'with' option (users: { with: { posts: N } }). Attempting to seed in the opposite direction (posts: { with: { users: N } }) will fail because it violates the relationship cardinality.
Drizzle Seed: 'with' cannot be used for self-referencing tables
Self-referencing tables (where a table references itself, such as a users table with a reportsTo field referencing users.id) cannot use the 'with' option for seeding, even if it's a one-to-many self-reference. Attempting to do so results in an error: 'table has self reference. You can't specify table as parameter in table.with object.'
Drizzle Seed 'with' option example: foreign key approach
Example showing how to seed users with posts using a foreign key reference:
Schema:
```ts
import { serial, pgTable, integer, text } from "drizzle-orm/pg-core";
export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name'),
});
export const posts = pgTable('posts', {
id: serial('id').primaryKey(),
content: text('content'),
authorId: integer('author_id').notNull().references(() => users.id),
});
```
Seeding:
```ts
import { users, posts } from './schema.ts';
async function main() {
const db = drizzle(...);
await seed(db, { users, posts }).refine(() => ({
users: {
count: 2,
with: {
posts: 3,
},
},
}));
}
main();
```
This generates 2 users, each with 3 posts.
Drizzle Seed 'with' option example: explicit relation approach
Example showing how to seed users with posts using an explicit one-to-many relation:
Schema:
```ts
import { serial, pgTable, integer, text } from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm";
export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name'),
});
export const posts = pgTable('posts', {
id: serial('id').primaryKey(),
content: text('content'),
authorId: integer('author_id').notNull(),
});
export const postsRelations = relations(posts, ({ one }) => ({
author: one(users, {
fields: [posts.authorId],
references: [users.id],
}),
}));
```
Seeding:
```ts
import { users, posts, postsRelations } from './schema.ts';
async function main() {
const db = drizzle(...);
await seed(db, { users, posts, postsRelations }).refine(() => ({
users: {
count: 2,
with: {
posts: 3,
},
},
}));
}
main();
```
This generates 2 users, each with 3 posts, using explicit relations instead of foreign keys.
Drizzle Seed: 'with' option supported across all databases
The 'with' option for seeding is supported on PostgreSQL, MySQL, SQLite, MSSQL, and Cockroach databases.