Skip to content

Add Smart Identifier Quoting for PostgreSQL Compatibility#7

Merged
tianzhou merged 1 commit intopgplex:mainfrom
screenfluent:feature/preserve-quoted-identifiers
Sep 6, 2025
Merged

Add Smart Identifier Quoting for PostgreSQL Compatibility#7
tianzhou merged 1 commit intopgplex:mainfrom
screenfluent:feature/preserve-quoted-identifiers

Conversation

@screenfluent
Copy link
Copy Markdown
Contributor

Add Smart Identifier Quoting for PostgreSQL Compatibility

Problem

pgschema currently strips quotes from identifiers, causing issues with:

  • Reserved words (e.g., user, order, group)
  • CamelCase/MixedCase identifiers (e.g., userId, createdAt)
  • Modern ORMs and auth libraries (Better Auth, Prisma, Drizzle)

PostgreSQL lowercases unquoted identifiers, so userId becomes userid, breaking compatibility with libraries expecting exact case.

Solution

This PR adds intelligent identifier quoting that preserves quotes when needed:

  • Reserved PostgreSQL keywords → quoted
  • Mixed case identifiers → quoted
  • Special characters → quoted
  • Simple lowercase → unquoted (no change)

Changes

  • Added identifier_quote.go with quoting logic
  • Updated getTableNameWithSchema() and qualifyEntityName() to use quoting
  • Modified table/column generation to preserve case
  • Added comprehensive tests

Impact

  • Better Auth users can use pgschema without field mapping
  • Prisma/Drizzle schemas work out-of-the-box
  • Backwards compatible - only quotes when necessary

Example

Before:

-- Input schema
CREATE TABLE "user" (
    "userId" TEXT,
    "createdAt" TIMESTAMP
);

-- pgschema output (broken)
CREATE TABLE user (  -- ERROR: reserved word
    userid TEXT,      -- wrong case
    createdat TIMESTAMP -- wrong case
);

After:

-- pgschema output (fixed)
CREATE TABLE "user" (
    "userId" TEXT,
    "createdAt" TIMESTAMP
);

Testing

  • Added unit tests for quoting logic
  • Tested with Better Auth schema
  • Backwards compatible with existing schemas

Fixes compatibility with: Better Auth, Supabase Auth, Lucia Auth, Prisma, Drizzle, TypeORM

- Add intelligent quoting for reserved words and mixed-case identifiers
- Preserve CamelCase for Better Auth, Prisma, Drizzle compatibility
- Quote PostgreSQL reserved words (user, order, group, etc.)
- Add comprehensive tests for quoting logic
- Backwards compatible - only quotes when necessary

Fixes compatibility with Better Auth, Supabase Auth, Lucia Auth, Prisma, Drizzle, TypeORM
Copy link
Copy Markdown
Contributor

@tianzhou tianzhou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for the PR!

@tianzhou tianzhou merged commit bf55dc7 into pgplex:main Sep 6, 2025
tianzhou added a commit that referenced this pull request Sep 6, 2025
column conditional quotation #7
tianzhou added a commit that referenced this pull request Sep 6, 2025
column conditional quotation #7
screenfluent added a commit to screenfluent/pgschema that referenced this pull request Sep 6, 2025
- Quote column names in UNIQUE, PRIMARY KEY, and FOREIGN KEY constraints
- Quote referenced table names in FOREIGN KEY constraints
- Add tests for constraint quoting with camelCase and reserved words
- Fixes issue where constraints with camelCase columns would fail

This completes the identifier quoting feature by covering constraints
that were missed in the initial PR pgplex#7.
tianzhou pushed a commit that referenced this pull request Sep 6, 2025
- Quote column names in UNIQUE, PRIMARY KEY, and FOREIGN KEY constraints
- Quote referenced table names in FOREIGN KEY constraints
- Add tests for constraint quoting with camelCase and reserved words
- Fixes issue where constraints with camelCase columns would fail

This completes the identifier quoting feature by covering constraints
that were missed in the initial PR #7.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants