Developer Tools

Generate UUIDs in Bulk for Database Seeds and Test Fixtures

7 min
By
Generate UUIDs in Bulk for Database Seeds and Test Fixtures

Photo by Pexels

The test suite needed 10,000 UUIDs for a fixture file. I wrote a script that called uuid.uuid4() in a loop, wrote each to a file, and waited. It took 40 seconds. A colleague did the same thing in 2 seconds. The difference was not the UUID generation. It was the I/O.

Generating UUIDs in bulk is easy. Generating them efficiently is not obvious. Here is what works.

Why You Need Bulk UUIDs

- **Database seed files.** You need stable UUIDs in a SQL seed file so foreign keys match across tables. Generating them once and hardcoding them is more reliable than generating at runtime. - **Test fixtures.** Unit and integration tests need deterministic data. Pre-generated UUIDs in a JSON or CSV fixture make tests reproducible. - **Load testing.** You need thousands of unique IDs to simulate users, orders, or sessions in a load test. - **Data migration.** Mapping old integer IDs to new UUIDs requires generating one per row, often in bulk.

The Slow Way

python # Slow — one file write per UUID import uuid

with open('uuids.txt', 'w') as f: for _ in range(10000): f.write(str(uuid.uuid4()) + '\n')

This is slow because of buffering, not UUID generation. Each f.write() may flush. On some systems, each line triggers a syscall.

The Fast Way

python # Fast — build in memory, write once import uuid

uuids = [str(uuid.uuid4()) for _ in range(10000)] with open('uuids.txt', 'w') as f: f.write('\n'.join(uuids))

This generates 10,000 UUIDs in under 50ms and writes them in a single I/O operation. The UUID generation itself is negligible. The bottleneck is always I/O.

Generate 100 UUIDs For A Quick Seed File

For small seed files, use our bulk UUID generator. Set the count to 100, pick v4, and copy the output. Paste it into your SQL file:

sql INSERT INTO users (id, email) VALUES ('f47ac10b-58cc-4372-a567-0e02b2c3d479', 'alice@example.com'), ('7c9e6f3a-2b1d-4e8c-a5f6-1d2e3f4a5b6c', 'bob@example.com'), ('3a2b1c8e-4d6a-7f5b-9e8c-1d2e3f4a5b6c', 'carol@example.com');

For 100 UUIDs this is fine. For 10,000, use a script.

UUID List Generator For Test Fixtures

Test fixtures need reproducible data. Two approaches:

**Approach 1: Pre-generate and commit.** Generate the UUIDs once, store them in a JSON fixture, and commit the file. Tests are fully deterministic. Downside: the fixture file is large and noisy in diffs.

json { "users": [ {"id": "f47ac10b-58cc-4372-a567-0e02b2c3d479", "name": "Alice"}, {"id": "7c9e6f3a-2b1d-4e8c-a5f6-1d2e3f4a5b6c", "name": "Bob"} ] }

**Approach 2: Seeded random.** Seed your random number generator with a fixed seed, generate UUIDs at test runtime. Tests are deterministic but the fixture file stays clean.

python import uuid import random

random.seed(42) # In Python, uuid4() uses os.urandom, not random, so you need a workaround # Use the uuid package with a custom RNG for seeded generation

Approach 1 is simpler and more debuggable. Use it unless your fixture files get unwieldy.

Node.js Bulk Generation

javascript const crypto = require('crypto');

const uuids = Array.from({ length: 10000 }, () => crypto.randomUUID()); require('fs').writeFileSync('uuids.txt', uuids.join('\n'));

10,000 UUIDs in under 100ms. The crypto.randomUUID() call is fast because it reads from the OS CSPRNG in bulk.

The SQL Seed Pattern

For database seeds, generate UUIDs and format them as SQL directly:

python import uuid

lines = [] for i in range(100): uid = uuid.uuid4() lines.append(f"('{uid}', 'user_{i}@example.com')")

sql = "INSERT INTO users (id, email) VALUES\n" + ',\n'.join(lines) + ';' with open('seed.sql', 'w') as f: f.write(sql)

This produces a ready-to-run SQL file with 100 rows of unique, properly formatted UUIDs.

Common Mistakes

**Reusing UUIDs across test runs without clearing the database.** If your seed file inserts UUID f47ac10b-58cc-4372-a567-0e02b2c3d479 and your test also inserts it, you get a constraint violation. Either clear the database between runs or use different UUID ranges for seeds vs. tests.

**Generating UUIDs in a hot loop during the test.** This makes tests slow and non-deterministic. Pre-generate.

**Using v1 for bulk generation on one machine.** v1 UUIDs from the same machine in the same nanosecond can collide if the clock sequence is not handled. Use v4 for bulk.

When To Use A UUID List Generator Tool

For anything under 1,000 UUIDs, an online tool is faster than writing a script. Our UUID generator handles bulk output. For anything over 10,000, a script is the right call because you need to write to a file and you want control over formatting.

For choosing UUID versions for your seed data, read UUID v1 vs v4 vs v5. For using those UUIDs as primary keys, see UUIDs as database primary keys.

Frequently Asked Questions

How many UUIDs can I generate at once?

Thousands per second in any language. The bottleneck is file I/O, not UUID generation. Build the list in memory and write once for the best performance.

Should I use v4 or v1 UUIDs for bulk generation?

Use v4. v1 UUIDs generated on the same machine in the same nanosecond can collide if the clock sequence is not managed correctly. v4 UUIDs have no such risk.

How do I make test fixtures with UUIDs reproducible?

Pre-generate the UUIDs once, store them in a JSON or SQL fixture file, and commit it. Tests load the fixture and get the same UUIDs every run.

Try NovelCrypt Tools

Experience military-grade encryption for your sensitive data. Create self-destructing messages, encrypt files, or explore our experimental lab tools.

Explore NovelCrypt