2
0 Comments

How to manually set table names in Prisma and why you should.

Since starting to work at Crunchy Data I have learned more about databases than I have my entire career.

Last week when showing off one of my projects backed by Prisma I got some dropped jaws when they looked at the database...😅

All across Prisma docs, tutorials, and content in the wild you will run into naming that looks like this for your models and fields:

  • User

  • createdAt

  • firstName

They encourage capitalized table names and camelcase fields/columns. However, this flies in the face of the conventions that most database experts would recommend:

If you are not a database expert you have probably never thought about this, and as a Javascript developer you probably still don’t care. That is not my domain as long as I can access my data what does it matter how they are named?

Conventions Matter

You’re a developer. You already know this is important. That is why tools like eslint, prettier, and others exist. Conventions make the parts of writing software that we all seem to enjoy wasting time on automatic.

Conventions also streamline onboarding. When you have an experienced developer joining the team, following industry standards allow that developer to onboard seamlessly into your team. If you have a junior joining having conventions helps them not only learn, but have confidence contributing.

It’s Not All Bad

Okay, but the people over at Prisma are smart, if the default way that they handle naming is SOOOO wrong why do they do it?

Developer Experience over Database Experience

One of benefits of using Prisma as an ORM is how they generate the client you use in your app based off of your schema. This means that their primary focus is on making the developer experience inside of your Typescript/Javascript application is great. This is why they recommend camelCase. It makes the generated code more in line with the conventions of TS/JS code.

Their focus is that the developer experience in your app is great not your experience in the database. They hope you never need to touch the database. However, any app built and used long enough will need to move past the app layer and care about the database, and if you haven’t taken care of how data is managed in your database you are going to have….fun.

They left a backdoor

They know that some people are going to care how their database is managed, they also need to account for people who are migrating existing databases in. So, Prisma left a backdoor when writing your schema to give you control over how your tables and fields are named.

How to manually set your Prisma table names

To explicitly set the table name in Prisma you use the @@map attribute. (docs)

model UserPurchase { 
  ...
  @@map("user_purchases")
}

How to manually set your Prisma field name

To explicitly set the field names in your Prisma models you will use the [@map](/map) attribute. (docs)

model Product {
  ...
  stripeProduct String [@map](/map)("stripe_product")
}

In Conclusion

The best thing that you can do for managing your data long term is to be consistent. Whether you are working by yourself, with a big team or the occasional contractor. Conventions and consistency = familiarity and speed.

Trending on Indie Hackers
Passed $7k 💵 in a month with my boring directory of job boards 35 comments Reaching $100k MRR Organically in 12 months 32 comments 87.7% of entrepreneurs struggle with at least one mental health issue 14 comments How to Secure #1 on Product Hunt: DO’s and DON'Ts / Experience from PitchBob – AI Pitch Deck Generator & Founders Co-Pilot 11 comments Competing with a substitute? 📌 Here are 4 ad examples you can use [from TOP to BOTTOM of funnel] 10 comments Are you wondering how to gain subscribers to a founder's X account from scratch? 9 comments