My journey

By trace

Creating a table for our Supabase database with Prisma in NextJs.

 ·

2 min read

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677876374611/6c31cac3-5c60-403d-8026-08004e95a763.jpeg align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677877291709/2a85d2df-85c7-486f-a74a-23805791d855.jpeg align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677877496996/47a80430-279d-4924-b072-6f866d459937.jpeg align="center")

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!