-
Create a new repository using this one as a template.
-
Clone down your repository.
-
Install the Prisma CLI.
npm install prisma --save-dev
-
Initialize Prisma to connect to PostgreSQL. You should see a new
prisma
folder, in which is a singleschema.prisma
file.
npx prisma init --datasource-provider postgresql
-
Create a new PostgreSQL database.
createdb prismatic-books
-
In the generated
.env
file, setDATABASE_URL
to"postgresql://username:password@localhost:5432/prismatic-books"
.- Make sure to change the username and password to your actual PostgreSQL credentials!
-
In the
schema.prisma
file, add aBook
model with the following fields:- the
id
is an integer that will auto-increment (see how to define an ID field) title
is a string
See solution
model Book { id Int @id @default(autoincrement()) title String }
- the
-
Create and run the initial migration.
npx prisma migrate dev --name init
- If successful, you should see the message "Your database is now in sync with your schema."
-
Explore the created database. You should see an empty
Book
model.
npx prisma studio
-
If you made a mistake or need to change the schema, use
npx prisma db push
to quickly sync your Prisma schema with your database schema. You want to run that command any time you change yourschema.prisma
file.- Once you are happy with your changes to the schema, run
npx prisma migrate dev
to generate a migration from your changes.
- Once you are happy with your changes to the schema, run