Skip to content

uuid_generate_v4() evaluated at table creation instead of runtime #472

@PabloPerezAguilo

Description

@PabloPerezAguilo

Describe the bug

noticed that when defining a table with a column like:

CREATE TABLE "organizations" (
"organization_id" uuid NOT NULL DEFAULT uuid_generate_v4(),
...
)

pg-mem calls the registered uuid_generate_v4 immediately during table creation, and logs a UUID. This means the column’s DEFAULT is set to that single fixed UUID, rather than a function that should be executed on each INSERT.

As a result:

The first insert works.

Any subsequent insert fails with a duplicate key, because the default UUID is reused.

In PostgreSQL real, DEFAULT uuid_generate_v4() executes at runtime for each insert, generating a new UUID every time.

It would be helpful if this behavior were either:

Adjusted to match PostgreSQL (call the function at runtime per insert), or

Explicitly documented as a known limitation, so users don’t get unexpected duplicate PKs.

Expected (not working)

import { randomUUID } from 'crypto';

db.public.registerFunction({
  name: 'uuid_generate_v4',
  returns: DataType.uuid,
  implementation: randomUUID,
});

It works:

import { randomUUID } from 'crypto';

db.public.registerFunction({
  name: 'uuid_generate_v4',
  returns: DataType.uuid,
  implementation: ()=>randomUUID,
});

pg-mem version

3.0.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions