When introspecting, turn lowercase plural table names into uppercase singular model names using `@@map`
See original GitHub issueIn the Prisma schema, model names are typically PascalCased. The main reason for this is consistency since Prisma’s base scalar types are all spelled in PascalCase notation. Another (rather subjective) reason is that PascalCasing probably feels more familiar to application developers as that’s typically also the notation for defining e.g. classes in code.
When introspecting a DB, I’d suggest to transform table names that are written in snake case to PascalCase, or of it’s just lowercase without underscores, just uppercase the first letter of the model name using @@map
.
Example
Assume this SQL schema:
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
title character varying(256) NOT NULL,
content text,
published boolean NOT NULL DEFAULT false,
"authorId" integer REFERENCES users(id)
);
CREATE UNIQUE INDEX posts_pkey ON posts(id int4_ops);
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name character varying(256),
email character varying(256) NOT NULL UNIQUE
);
CREATE UNIQUE INDEX users_pkey ON users(id int4_ops);
CREATE UNIQUE INDEX users_email_key ON users(email text_ops);
With the current introspection, this results in the following Prisma schema:
model posts {
authorId users?
content String?
id Int @id
published Boolean @default(false)
title String
}
model users {
email String @unique
id Int @id
name String?
postses posts[]
}
A few things feel a bit off in that Prisma schema:
- The types of all fields are uppercased except for the relation fields
authorId
is a to-one-relation but links to a pluralized fieldposts
got one more additional pluralization topostses
- The type of
postses
isposts[]
which seems like it could be a list of a list of posts (basically a 2-dimensional array)
Instead, I suggest that we should try to adjust the introspection such that the following Prisma schema is generated:
model Post {
authorId User?
content String?
id Int @id
published Boolean @default(false)
title String
@@map("posts")
}
model User {
email String @unique
id Int @id
name String?
posts Post[]
@@map("users")
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:39
- Comments:9 (5 by maintainers)
Oh so the whole schema is not overwritten every time I do a
prisma db pull
? If so that’s really great, and this indeed solves my problem once the initial setup is done.I think so, it wasn’t obvious as usually this kind of command generates a whole new file. Having a mix of both generated code and static code isn’t very common, so it could be useful to document it 😉