{"id":"eea3bbee139ca6c0","repo":"drizzle-team/drizzle-orm","slug":"you-can-t-generate-count-unique-strings-with-a","errorCode":null,"errorMessage":"You can't generate ${count} unique strings, with a maximum string length of ${maxStringLength}.","messagePattern":"You can't generate (.+?) unique strings, with a maximum string length of (.+?)\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"drizzle-seed/src/services/versioning/v2.ts","lineNumber":194,"sourceCode":"\t\trng: prand.RandomGenerator;\n\t\tminStringLength: number;\n\t\tmaxStringLength: number;\n\t} | undefined;\n\tpublic override isUnique = true;\n\n\toverride init({ seed, count }: { seed: number; count: number }) {\n\t\tconst rng = prand.xoroshiro128plus(seed);\n\n\t\tlet minStringLength = 7;\n\t\tlet maxStringLength = 20;\n\t\t// TODO: revise later\n\t\tif (this.stringLength !== undefined) {\n\t\t\tmaxStringLength = this.stringLength;\n\t\t\tif (maxStringLength === 1 || maxStringLength < minStringLength) minStringLength = maxStringLength;\n\t\t}\n\n\t\tif (maxStringLength < count.toString(16).length) {\n\t\t\tthrow new Error(\n\t\t\t\t`You can't generate ${count} unique strings, with a maximum string length of ${maxStringLength}.`,\n\t\t\t);\n\t\t}\n\n\t\tthis.state = { rng, minStringLength, maxStringLength };\n\t}\n\n\tgenerate({ i }: { i: number }) {\n\t\tif (this.state === undefined) {\n\t\t\tthrow new Error('state is not defined.');\n\t\t}\n\n\t\tconst minStringLength = this.state.minStringLength,\n\t\t\tmaxStringLength = this.state.maxStringLength;\n\t\tconst stringChars = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';\n\t\tlet idx: number,\n\t\t\tstrLength: number;\n\t\tlet currStr: string;","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/drizzle-team/drizzle-orm/blob/b7862528fd8fc39bc2653a6c18dad7c1f4e68d10/drizzle-seed/src/services/versioning/v2.ts#L176-L212","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Widen the column's varchar length to at least count.toString(16).length + 1.","Reduce count so its hex representation fits within maxStringLength.","Remove the unique constraint if uniqueness is not required, allowing the non-unique string generator."],"exampleFix":"// before — varchar(2) unique, count 1000 (hex '3e8' = 3 chars)\ncode: varchar('code', { length: 2 }).notNull().unique()\nawait seed(db, { schema }, { count: 1000 });\n// after — widen to fit hex of count\ncode: varchar('code', { length: 8 }).notNull().unique()","handlingStrategy":"validation","validationCode":"// GenerateUniqueStringV2 needs maxStringLength >= count.toString(16).length.\nfunction maxStringLengthFitsUniqueCount(maxStringLength: number, count: number): boolean {\n  return maxStringLength >= count.toString(16).length;\n}\n// Usage before seeding:\n// if (!maxStringLengthFitsUniqueCount(colLength, count)) widen column or reduce count.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure unique string columns have a varchar length >= the hex width of your max count (e.g. length >= 8 for counts up to ~4 billion).","Reduce count for narrow unique-code columns.","Remove the unique constraint if uniqueness is not required."],"tags":["unique","string","string-length","count-limit","v2"],"analyzedSha":"b7862528fd8fc39bc2653a6c18dad7c1f4e68d10","analyzedAt":"2026-08-03T18:11:14.318Z","schemaVersion":2}