prisma1 icon indicating copy to clipboard operation
prisma1 copied to clipboard

updateManyXXXX not working as expected for complex type in schema

Open thanatchakromsang opened this issue 8 years ago • 5 comments

Describe the bug Can't updateManyXXXX fields null to post types It only work with basic type

To Reproduce Steps to reproduce the behavior:

  1. Deploy this schema
type User {
  id: ID! @unique
  createdAt: DateTime!
  updatedAt: DateTime!
  username: String! @unique
  password: String!
}
  1. Create User from mutation
  2. Add more fields to schema
type User {
  id: ID! @unique
  createdAt: DateTime!
  updatedAt: DateTime!
  username: String! @unique
  password: String!
  post: Post
}

type Post {
  id: ID! @unique
  createdAt: DateTime!
  updatedAt: DateTime!
  content: String
}

  1. Now post fields in old user will be null but we want to migrate old data to have post type
mutation {
  migration_01: updateManyUsers(
    where: { post: null }
    data: {
      post: {
        create: {
          content: "hello world"
        }
      }
    }
  ) {
    count
  }
}

Expected behavior Return post not null and have content = "hello world" for that user instead post will still be null and no content

Versions (please complete the following information):

  • OS: Linux archlinux 4.18.5-arch1-1-ARCH
  • prisma CLI: prisma/1.16 node-v8.11.4
  • Prisma Server: 1.16

thanatchakromsang avatar Sep 14 '18 18:09 thanatchakromsang

@thanatchakromsang I have same your problem. I want to know a solution for migration this case.

colevels avatar Sep 15 '18 03:09 colevels

I don't understand the expected behaviour you're proposing, can you clarify?

marktani avatar Sep 18 '18 06:09 marktani

@marktani I want to migrate old users which have Post as null but in reproduction step 4 It did nothing

thanatchakromsang avatar Sep 18 '18 07:09 thanatchakromsang

I see! This is because nested mutations inside updateMany are currently not applied: #2010. Please follow the other issue 🙂

marktani avatar Sep 18 '18 07:09 marktani

@marktani Thanks for the head up I got it working by migration script using typescript instead

import { Prisma } from '../generated/prisma'

const prisma = new Prisma({
  endpoint: process.env.PRISMA_ENDPOINT,
  secret: process.env.PRISMA_SECRET,
  debug: false,
})

const main() = async () => {
  const allUser = await prisma.query.users({
    where: { notificationSetting: null },
  })
  let count = 0
  const updatedUser = await Promise.all(
    allUser.map(async user => {
      count += 1
      console.log(`update user id: ${user.id}`)
      return prisma.mutation.updateUser({
        where: { id: user.id },
        data: {
          post: {
            create: {
              content: "hello world",
            },
          },
        },
      })
    }),
  )
  console.log(`===== updated ${count} users =====`)
}

main()
  .then(() => {
    process.exit(0)
  })
  .catch(e => {
    console.log(e)
    process.exit(1)
  })

thanatchakromsang avatar Sep 18 '18 16:09 thanatchakromsang