Creating a table for our Supabase database with Prisma in NextJs.
In this article, we will go through how we can define a data model in the schema.prisma file and then create a table in supabase with Prisma migration.
Before we create a model, we need to connect our application to supabase with the help of Prisma. If you have already done that, you can follow along; else you need to connect it. To connect, check out my other article: How to connect a NextJs application to Supabase with Prisma?
For this article, we will be defining a model called "User" to create a table with the following properties: id, first name, last name, city, email, and password. This is a basic user model that can be used in any application. All our properties will be a string except id which will be integer.
Now we need to go to the NextJs application folder --> prisma --> schema.prisma
Below the data source db, we need to define our model. All we need to do it type "model" and then the model name, in our case "User".
model User {
id Int @id @default(autoincrement())
first_name String
last_name String
city String
email String @unique
password String
created_at DateTime @default(now())
updated_at DateTime @updatedAt
}
We need to specify the type of our input fields.
After we define our table, we need to see it in our database, for which we will need to use the following command: "npx prisma db push
". This will sync our database with our defined schema. Once you run the command, you should see something like the following snapshot.
Tip: Always restart your server after you make changes to your env file.
Now, to verify, we can go to our supabase project. Then click on Table Editor [left task bar]. The "User" table with the properties we defined should be visible.
That's all there is to it; we have now successfully defined a "User" model(table) for the supabase database with the help of Prisma.
I hope this article helped you.
Happy hacking!