Skip to content

fix: rename native type mappings to match datasource during Prisma schema generation#2760

Open
sanny-io wants to merge 5 commits into
zenstackhq:devfrom
sanny-io:fix/prisma-schema-gen-ntm
Open

fix: rename native type mappings to match datasource during Prisma schema generation#2760
sanny-io wants to merge 5 commits into
zenstackhq:devfrom
sanny-io:fix/prisma-schema-gen-ntm

Conversation

@sanny-io

@sanny-io sanny-io commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Prisma's native type mappings rely on the datasource name. This is a problem because ZenStack uses a hardcoded prefix of @db.* for native type mappings.

For example, the following schema is valid in ZenStack, but invalid in Prisma. Migration commands will fail.

datasource ds {
    provider = 'postgresql'
    url      = env('POSTGRES_URL')
}

model User {
    id String @id @db.Text // it must be `@ds.Text` for Prisma
}

This change renames native type mappings to match the datasource during Prisma schema generation. An alternative to this is renaming the datasource itself, but since that's user-defined, I thought this was less invasive.

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes
    • Prisma schema generation now rewrites native type mapping attributes to use the active datasource alias (for example, @ds.Text, @ds.Boolean, @ds.JsonB, @ds.Timestamptz(3), @ds.ByteA) instead of emitting @db.*.
  • Tests
    • Added a PostgreSQL-focused test to verify @db.* mappings are not produced and the corresponding @ds.* mappings are present.

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 9572e124-a635-401b-9287-32ec4f03ef32

📥 Commits

Reviewing files that changed from the base of the PR and between 2c26e6e and 6b48f0a.

📒 Files selected for processing (1)
  • packages/sdk/src/prisma/prisma-schema-generator.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/sdk/src/prisma/prisma-schema-generator.ts

📝 Walkthrough

Walkthrough

Prisma schema generation now rewrites native type mappings from @db.* to the active datasource alias and adds a PostgreSQL-focused test covering the generated mappings.

Changes

Native type mapping generation

Layer / File(s) Summary
Datasource-qualified native mappings
packages/sdk/src/prisma/prisma-schema-generator.ts, packages/cli/test/prisma-schema-gen.test.ts
PrismaSchemaGenerator detects @db.* attributes, rewrites them using the datasource name, and tests the resulting @ds.* mappings while excluding the original markers.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the core change: renaming native type mappings to use the datasource name during Prisma schema generation.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/sdk/src/prisma/prisma-schema-generator.ts (1)

343-350: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Add missing semicolon.

For consistency with the rest of the file, consider adding a semicolon at the end of this statement.

♻️ Proposed refactor
         if (this.isNativeTypeMappingAttribute(attr)) {
             const dataSource = this.zmodel.declarations.find(isDataSource);
             if (dataSource) {
-                attrName = attrName.replace('`@db`', `@${dataSource.name}`)
+                attrName = attrName.replace('`@db`', `@${dataSource.name}`);
             }
         }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/sdk/src/prisma/prisma-schema-generator.ts` around lines 343 - 350,
Add the missing semicolon to the attrName reassignment statement in
makeFieldAttribute, matching the file’s existing statement style.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/sdk/src/prisma/prisma-schema-generator.ts`:
- Around line 343-350: Add the missing semicolon to the attrName reassignment
statement in makeFieldAttribute, matching the file’s existing statement style.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e7af160b-8b8a-43cc-a62f-fe2352772790

📥 Commits

Reviewing files that changed from the base of the PR and between 7767140 and a35cb3f.

📒 Files selected for processing (2)
  • packages/cli/test/prisma-schema-gen.test.ts
  • packages/sdk/src/prisma/prisma-schema-generator.ts

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.

1 participant