Get Started

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! @unique
  email: String @unique
  name: String!
  posts: [Post!]!
}

type Post {
  id: ID! @unique
  title: String!
  published: Boolean! @default(value: "false")
  author: User
}
Copy

Here's what changed:

  • You added a new email field to the User type.
  • You added a new Post type to the datamodel.
  • You added a relation between User and Post (via the posts and author fields).

Redeploy your Prisma API

To apply the changes you just made to your datamodel, you need to redeploy the Prisma API:

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.

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-prisma/generated/prisma-client"
)

func main() {
    client := prisma.New(nil)
    ctx := context.TODO()

    // Create a new user with two posts
    name := "Alice"
    email := "alice@prisma.io"
    title1 := "Join us for GraphQL Conf in 2018"
    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-prisma/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
Fantastic! 🎉 You are now able to migrate your database using Prisma. Next you will learn how to build an API server based on Prisma client.
Next Step