Change Datamodel
Goals
On this page, you will learn how to:
- Adjust the datamodel and update your Prisma API
- Regenerate your Prisma client
- Create two database records in a single transaction
- Query relational data
This page is only relevant if you started with a new database or a Demo server. If you configured Prisma with an existing database, you need to run migrations directly against your database.
Adjust your datamodel
Update the datamodel in datamodel.prisma
as follows:
type User { id: ID! @id email: String @unique name: String! posts: [Post!]! } type Post { id: ID! @id title: String! published: Boolean! @default(value: false) author: User @relation(link: INLINE) }
Copy
Here's what changed:
- You added a new
email
field to theUser
type. - You added a new
Post
type to the datamodel. - You added a relation between
User
andPost
(via theposts
andauthor
fields).
Redeploy your Prisma API
To apply the changes you just made to your datamodel, you need to redeploy the Prisma datamodel:
prisma deploy
Copy
Regenerate your Prisma client
Because the Prisma client is based on your datamodel, it needs to be regenerated every time the datamodel is updated:
prisma generate
Copy
The Prisma client library in the /generated/prisma-client
directory is now being updated and its API has been adjusted to use the new datamodel.
You can ensure that your Prisma client is automatically being updated after every deploy by adding the following lines to your prisma.yml
:
hooks: post-deploy: - prisma generate
Copy
Read and write nested objects
The Prisma client API allows to write nested objects in a single transaction without having to manually control when the transaction starts or ends.
Update your index.go
to look as follows:
package main import ( "context" "fmt" prisma "hello-world/generated/prisma-client" ) func main() { client := prisma.New(nil) ctx := context.TODO() // Create a new user with two posts name := "Bob" email := "bob@prisma.io" title1 := "Join us for GraphQL Conf in 2019" title2 := "Subscribe to GraphQL Weekly for GraphQL news" newUser, err := client.CreateUser(prisma.UserCreateInput{ Name: name, Email: &email, Posts: &prisma.PostCreateManyWithoutAuthorInput{ Create: []prisma.PostCreateWithoutAuthorInput{ prisma.PostCreateWithoutAuthorInput{ Title: title1, }, prisma.PostCreateWithoutAuthorInput{ Title: title2, }, }, }, }).Exec(ctx) if err != nil { panic(err) } fmt.Printf("Created new user: %+v\n", newUser) allUsers, err := client.Users(nil).Exec(ctx) if err != nil { panic(err) } fmt.Printf("%+v\n", allUsers) allPosts, err := client.Posts(nil).Exec(ctx) if err != nil { panic(err) } fmt.Printf("%+v\n", allPosts) }
Copy
Run the script with the following command:
go run index.go
Copy
Query relational data
With the Prisma client API, you can navigate relations in your data graph using chained method calls (also called fluent API). Here is how you can query the posts
written by a certain User
:
package main import ( "context" "fmt" prisma "hello-world/generated/prisma-client" ) func main() { client := prisma.New(nil) ctx := context.TODO() email := "alice@prisma.io" postsByUser, err := client.User(prisma.UserWhereUniqueInput{ Email: &email, }).Posts(nil).Exec(ctx) if err != nil { panic(err) } fmt.Printf("%+v\n", postsByUser) }
Copy