drizzle-team/drizzle-orm · error · Error

You can't generate ${count} unique strings, with a maximum s

Error message

You can't generate ${count} unique strings, with a maximum string length of ${maxStringLength}.

What it means

GenerateUniqueStringV2 generates unique strings by encoding the row index in hexadecimal and padding it to a random length up to maxStringLength. If maxStringLength is so small that it cannot even hold the hex representation of count (i.e. maxStringLength < count.toString(16).length), uniqueness cannot be guaranteed and init() throws.

Source

Thrown at drizzle-seed/src/services/versioning/v2.ts:194

		rng: prand.RandomGenerator;
		minStringLength: number;
		maxStringLength: number;
	} | undefined;
	public override isUnique = true;

	override init({ seed, count }: { seed: number; count: number }) {
		const rng = prand.xoroshiro128plus(seed);

		let minStringLength = 7;
		let maxStringLength = 20;
		// TODO: revise later
		if (this.stringLength !== undefined) {
			maxStringLength = this.stringLength;
			if (maxStringLength === 1 || maxStringLength < minStringLength) minStringLength = maxStringLength;
		}

		if (maxStringLength < count.toString(16).length) {
			throw new Error(
				`You can't generate ${count} unique strings, with a maximum string length of ${maxStringLength}.`,
			);
		}

		this.state = { rng, minStringLength, maxStringLength };
	}

	generate({ i }: { i: number }) {
		if (this.state === undefined) {
			throw new Error('state is not defined.');
		}

		const minStringLength = this.state.minStringLength,
			maxStringLength = this.state.maxStringLength;
		const stringChars = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		let idx: number,
			strLength: number;
		let currStr: string;

View on GitHub (pinned to b7862528fd)

Solutions

  1. Widen the column's varchar length to at least count.toString(16).length + 1.
  2. Reduce count so its hex representation fits within maxStringLength.
  3. Remove the unique constraint if uniqueness is not required, allowing the non-unique string generator.

Example fix

// before — varchar(2) unique, count 1000 (hex '3e8' = 3 chars)
code: varchar('code', { length: 2 }).notNull().unique()
await seed(db, { schema }, { count: 1000 });
// after — widen to fit hex of count
code: varchar('code', { length: 8 }).notNull().unique()
Defensive patterns

Strategy: validation

Validate before calling

// GenerateUniqueStringV2 needs maxStringLength >= count.toString(16).length.
function maxStringLengthFitsUniqueCount(maxStringLength: number, count: number): boolean {
  return maxStringLength >= count.toString(16).length;
}
// Usage before seeding:
// if (!maxStringLengthFitsUniqueCount(colLength, count)) widen column or reduce count.

Prevention

When it happens

Trigger: A unique string column with a very short varchar length (e.g. varchar(2)) combined with a count whose hex representation is longer than the column; e.g. count=1000 needs at least 3 hex chars so a length of 2 fails.

Common situations: Short-code columns (e.g. 2-char codes) that must be unique but are seeded with more rows than the alphabet space allows; narrow unique slug columns.

Related errors


AI-assisted analysis of drizzle-team/drizzle-orm@b7862528fd (2026-08-03). Data as JSON: /data/errors/eea3bbee139ca6c0.json. Report an issue: GitHub.