normalize-schema.json
normalize-schema.json
normalize-schema.json lets you rename ugly auto-generated relation field names in your Prisma schema after each prisma db pull. Place it next to schema.prisma in the datasource's prisma/<name>/ folder.
Why
When Prisma introspects a database with ambiguous or composite foreign keys it generates deterministic but ugly field names β Work_author_idToAuthor, annotation_annotation_idToAnnotation, etc. You can't control these via Prisma config, and they leak into generated Zod types and your code. normalize-schema.json is a one-time rename map that runs automatically as part of crouton update resources (between prisma-case-format and prisma generate), so every pull keeps clean names without manual edits.
File location
prisma/
βββ <datasource-name>/
βββ schema.prisma
βββ prisma.config.ts
βββ normalize-schema.json β hereFormat
{
"renames": {
"ModelName": {
"uglyFieldName": "cleanFieldName"
}
}
}renamesβ map of Prisma model names to field rename maps.- Each key inside a model is the current field name in the schema (after
prisma-case-formathas run); the value is the name you want, or{ "to": "name", "relationOnly": true }to rename only the relation field (see Renaming only the relation field).
Example
Given a schema with:
model Work {
id Int @id
Work_author_idToAuthor Author @relation("Work_author_idToAuthor", fields: [author_id], references: [id])
Work_reviewer_idToAuthor Author @relation("Work_reviewer_idToAuthor", fields: [reviewer_id], references: [id])
}Add this alongside schema.prisma:
{
"renames": {
"Work": {
"Work_author_idToAuthor": "author",
"Work_reviewer_idToAuthor": "reviewer"
}
}
}After the next crouton update resources the schema becomes:
model Work {
id Int @id
author Author @relation("Work_author_idToAuthor", fields: [author_id], references: [id])
reviewer Author @relation("Work_reviewer_idToAuthor", fields: [reviewer_id], references: [id])
}The @relation name and fields/references are untouched β only the field name changes.
Renaming only the relation field
A string rename matches every field line with that name in the model. When case-formatting gives a scalar and a relation the same name (e.g. column revision_status and FK revision_status_id β revision_status both become revisionStatus), use the object form with relationOnly to rename just the relation field:
{
"renames": {
"Text": {
"revisionStatus": { "to": "revisionStatusRel", "relationOnly": true }
}
}
}model Text {
revisionStatus String?
revisionStatusId Int?
revisionStatusRel RevisionStatus? @relation(fields: [revisionStatusId], references: [id])
}A field counts as a relation when its type is a model in the schema or the line contains @relation(. Scalar lines are left alone. { "to": "..." } without relationOnly behaves like the string form.
Tips
You only need this to pick your own name. If you do nothing, crouton update resources resolves such collisions automatically after normalizeSchema by renaming the relation field to <name>Ref (e.g. revisionStatusRef).
When it runs
normalizeSchema runs as step 4 of crouton update resources:
prisma db pullβ introspect from the live databaseprisma-case-formatβ PascalCase models, camelCase fieldsnormalizeSchemaβ apply renames from this file β hereprisma generateβ regenerate Zod types from the cleaned schema
It also runs inside the crouton-prisma generator itself (prisma generate), so the temp schema used for Zod generation always has clean names even if you invoke prisma generate directly.
Keeping it in sync
After a prisma db pull that adds new relation fields, re-check whether the generated names are ugly and add entries to the file. Existing entries are applied on every pull, so you only need to add new ones β old ones are harmless even if the field was already clean.
Tips
Rename map keys must match the field name as it appears after prisma-case-format runs (camelCase), not the raw introspected name (snake_case).