question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When introspecting, turn lowercase plural table names into uppercase singular model names using `@@map`

See original GitHub issue

In 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 field
  • posts got one more additional pluralization to postses
  • The type of postses is posts[] 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:open
  • Created 4 years ago
  • Reactions:39
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
clement-estonecommented, Mar 30, 2022

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.

0reactions
clement-estonecommented, Mar 31, 2022

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 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

When introspecting, turn lowercase plural table names into ...
When introspecting, turn lowercase plural table names into uppercase singular model names using @@map #2502.
Read more >
Names in the underlying database
A common approach for naming tables/collections in databases for example is to use plural form and snake_case notation. Prisma on the other hand...
Read more >
How to make Sequelize use singular table names
If you require to have different model names for singuar and plural definitions you can pass name as a parameter in options of...
Read more >
ActiveSupport::Inflector - Rails API
The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without, and class names to...
Read more >
Hibernate ORM 5.4.33.Final User Guide
in mapping the Contact table, all attributes except for name would be basic types. ... Basic value types usually map a single database...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found