How to generate UUIDs in Prisma
October 22, 2023
UUIDs (Universally Unique Identifiers) are often used for uniquely identifying records in a database. Prisma makes it incredibly easy to use UUIDs for this purpose. In this guide, we'll go over how to generate and use UUIDs in Prisma.
For a detailed comparison of UUIDs versus other ID methods, check out our other post comparing UUID vs GUID vs CUID vs NanoID for database primary keys.
Requirements
- Node.js
- Prisma CLI
- A database that supports UUIDs (e.g., PostgreSQL, MySQL 8.0+)
Setup Your Prisma Schema
Firstly, you'll want to specify that a field should be a UUID in your Prisma schema. Open schema.prisma
and modify it like this:
model User { id String @id @default(uuid()) name String }
Here, the id
field is set to be a UUID. The @default(uuid())
directive tells Prisma to generate a UUID by default.
Migrate Database
Apply the migration to update the database schema.
prisma migrate dev --name init
Insert Data
When you create a new user, Prisma will automatically populate the id
field with a UUID.
const { PrismaClient } = require('@prisma/client') const prisma = new PrismaClient() async function main() { const newUser = await prisma.user.create({ data: { name: 'John Doe', }, }) console.log(`Created new user: ${newUser}`) }
You could ship faster.
Imagine the time you'd save if you never had to build another internal tool, write a SQL report, or manage another admin panel again. Basedash is built by internal tool builders, for internal tool builders. Our mission is to change the way developers work, so you can focus on building your product.
Retrieve Data
Retrieving data remains unchanged. You can query the id
field like any other field.
const user = await prisma.user.findUnique({ where: { id: 'some-uuid', }, })
Update Data
You can also update a UUID field if needed, though this is generally not recommended for fields serving as primary keys.
const updatedUser = await prisma.user.update({ where: { id: 'some-uuid' }, data: { id: 'new-uuid' }, })
Tips
- For PostgreSQL, consider using the native
uuid-ossp
extension for UUID generation. You can enable it via a Prisma migration.
-- Enable UUID generation in PostgreSQL CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
- For MySQL 8.0+, UUIDs are supported natively, and no additional configuration is needed.
- For SQLite, you will have to generate the UUID in your application code, as SQLite doesn't natively support UUID generation.
Conclusion
That's it. You've learned how to set up, insert, query, and update UUIDs in Prisma. It's a straightforward process that adds a layer of uniqueness and complexity to your data models.
TOC
October 22, 2023
UUIDs (Universally Unique Identifiers) are often used for uniquely identifying records in a database. Prisma makes it incredibly easy to use UUIDs for this purpose. In this guide, we'll go over how to generate and use UUIDs in Prisma.
For a detailed comparison of UUIDs versus other ID methods, check out our other post comparing UUID vs GUID vs CUID vs NanoID for database primary keys.
Requirements
- Node.js
- Prisma CLI
- A database that supports UUIDs (e.g., PostgreSQL, MySQL 8.0+)
Setup Your Prisma Schema
Firstly, you'll want to specify that a field should be a UUID in your Prisma schema. Open schema.prisma
and modify it like this:
model User { id String @id @default(uuid()) name String }
Here, the id
field is set to be a UUID. The @default(uuid())
directive tells Prisma to generate a UUID by default.
Migrate Database
Apply the migration to update the database schema.
prisma migrate dev --name init
Insert Data
When you create a new user, Prisma will automatically populate the id
field with a UUID.
const { PrismaClient } = require('@prisma/client') const prisma = new PrismaClient() async function main() { const newUser = await prisma.user.create({ data: { name: 'John Doe', }, }) console.log(`Created new user: ${newUser}`) }
You could ship faster.
Imagine the time you'd save if you never had to build another internal tool, write a SQL report, or manage another admin panel again. Basedash is built by internal tool builders, for internal tool builders. Our mission is to change the way developers work, so you can focus on building your product.
Retrieve Data
Retrieving data remains unchanged. You can query the id
field like any other field.
const user = await prisma.user.findUnique({ where: { id: 'some-uuid', }, })
Update Data
You can also update a UUID field if needed, though this is generally not recommended for fields serving as primary keys.
const updatedUser = await prisma.user.update({ where: { id: 'some-uuid' }, data: { id: 'new-uuid' }, })
Tips
- For PostgreSQL, consider using the native
uuid-ossp
extension for UUID generation. You can enable it via a Prisma migration.
-- Enable UUID generation in PostgreSQL CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
- For MySQL 8.0+, UUIDs are supported natively, and no additional configuration is needed.
- For SQLite, you will have to generate the UUID in your application code, as SQLite doesn't natively support UUID generation.
Conclusion
That's it. You've learned how to set up, insert, query, and update UUIDs in Prisma. It's a straightforward process that adds a layer of uniqueness and complexity to your data models.
October 22, 2023
UUIDs (Universally Unique Identifiers) are often used for uniquely identifying records in a database. Prisma makes it incredibly easy to use UUIDs for this purpose. In this guide, we'll go over how to generate and use UUIDs in Prisma.
For a detailed comparison of UUIDs versus other ID methods, check out our other post comparing UUID vs GUID vs CUID vs NanoID for database primary keys.
Requirements
- Node.js
- Prisma CLI
- A database that supports UUIDs (e.g., PostgreSQL, MySQL 8.0+)
Setup Your Prisma Schema
Firstly, you'll want to specify that a field should be a UUID in your Prisma schema. Open schema.prisma
and modify it like this:
model User { id String @id @default(uuid()) name String }
Here, the id
field is set to be a UUID. The @default(uuid())
directive tells Prisma to generate a UUID by default.
Migrate Database
Apply the migration to update the database schema.
prisma migrate dev --name init
Insert Data
When you create a new user, Prisma will automatically populate the id
field with a UUID.
const { PrismaClient } = require('@prisma/client') const prisma = new PrismaClient() async function main() { const newUser = await prisma.user.create({ data: { name: 'John Doe', }, }) console.log(`Created new user: ${newUser}`) }
You could ship faster.
Imagine the time you'd save if you never had to build another internal tool, write a SQL report, or manage another admin panel again. Basedash is built by internal tool builders, for internal tool builders. Our mission is to change the way developers work, so you can focus on building your product.
Retrieve Data
Retrieving data remains unchanged. You can query the id
field like any other field.
const user = await prisma.user.findUnique({ where: { id: 'some-uuid', }, })
Update Data
You can also update a UUID field if needed, though this is generally not recommended for fields serving as primary keys.
const updatedUser = await prisma.user.update({ where: { id: 'some-uuid' }, data: { id: 'new-uuid' }, })
Tips
- For PostgreSQL, consider using the native
uuid-ossp
extension for UUID generation. You can enable it via a Prisma migration.
-- Enable UUID generation in PostgreSQL CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
- For MySQL 8.0+, UUIDs are supported natively, and no additional configuration is needed.
- For SQLite, you will have to generate the UUID in your application code, as SQLite doesn't natively support UUID generation.
Conclusion
That's it. You've learned how to set up, insert, query, and update UUIDs in Prisma. It's a straightforward process that adds a layer of uniqueness and complexity to your data models.
What is Basedash?
What is Basedash?
What is Basedash?
Basedash 🤝 Prisma
Teams that build with Prisma love using Basedash to manage and administrate their databases. It fits in your stack and allows you to understand your data like never before.
Teams that build with Prisma love using Basedash to manage and administrate their databases. It fits in your stack and allows you to understand your data like never before.
Teams that build with Prisma love using Basedash to manage and administrate their databases. It fits in your stack and allows you to understand your data like never before.
Dashboards and charts
Edit data, create records, oversee how your product is running without the need to build or manage custom software.
USER CRM
ADMIN PANEL
SQL COMPOSER WITH AI
Related posts
Related posts
Related posts
How to automate Prisma migrations in a CI/CD pipeline
Max Musing
How to implement soft deletes in Prisma
Max Musing
How to use the shadow database in Prisma
Kris Lachance
How to reset and seed a Prisma database
Max Musing
UUID vs GUID vs CUID vs NanoID: A guide to database primary keys
Max Musing
How to squash migrations in Prisma
Max Musing