Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upsert nested relations only applied to the last relation #1080

Closed
damiankuriata opened this issue Mar 5, 2024 · 3 comments
Closed

Upsert nested relations only applied to the last relation #1080

damiankuriata opened this issue Mar 5, 2024 · 3 comments
Milestone

Comments

@damiankuriata
Copy link

Description and expected behavior
The upsert operation on multiple nested relations is applied only to the last relation. Works fine with pure PrismaClient.

Environment (please complete the following information):

  • ZenStack version: 1.10.0
  • Prisma version: 5.10.2
  • Database type: postgres

Additional context

Data model:

generator client {
    provider = "prisma-client-js"
}

datasource db {
    provider  = "postgresql"
    url      = env("DATABASE_URL")
}

model Project {
    id String @id @unique @default(uuid())
    Fields Field[]

    @@allow('all', true)
}

model Field {
    id String @id @unique @default(uuid())
    name String
    Project Project @relation(fields: [projectId], references: [id])
    projectId String

    @@allow('all', true)
}

Code:

const project = await prisma.project.create({
    include: { Fields: true },
    data: {
        Fields: {
            create: [
                { name: 'first' },
                { name: 'second' },
            ]
        }
    },
});
console.log(project);

const updated = await prisma.project.update({
    where: { id: project.id },
    include: { Fields: true },
    data: {
        Fields: {
            upsert: [
                {
                    where: { id: project.Fields[0].id },
                    create: { name: 'firstXXX' },
                    update: { name: 'firstXXX' }
                },
                {
                    where: { id: project.Fields[1].id },
                    create: { name: 'secondXXX' },
                    update: { name: 'secondXXX' }
                }
            ]
        }
    }
})
console.log(updated);

When using pure PrismaClient this is the result

{
  id: '1',
  Fields: [
    {
      id: 'd8aaa0f1-3f6a-4f21-ae7d-71b9685b4598',
      name: 'first', <-------------------------------------------------------------- ORIGINAL NAME
      projectId: '1'
    },
    {
      id: 'bb2e15b3-35b1-443e-8db5-e412c10fca94',
      name: 'second', <------------------------------------------------------------- ORIGINAL NAME
      projectId: '1'
    }
  ]
}
{
  id: '1',
  Fields: [
    {
      id: 'd8aaa0f1-3f6a-4f21-ae7d-71b9685b4598',
      name: 'firstXXX', <----------------------------------------------------------- UPDATED NAME
      projectId: '1'
    },
    {
      id: 'bb2e15b3-35b1-443e-8db5-e412c10fca94',
      name: 'secondXXX', <---------------------------------------------------------- UPDATED NAME
      projectId: '1'
    }
  ]
}

but with enhanced prisma client, this is the result

{
  id: '1',
  Fields: [
    {
      id: 'b12b4216-c190-404d-844b-47d31d7917e7',
      name: 'first', <-------------------------------------------------------------- ORIGINAL NAME
      projectId: '1'
    },
    {
      id: '004fe98e-c1e3-4c28-bf6b-fc76463c5257',
      name: 'second', <------------------------------------------------------------- ORIGINAL NAME
      projectId: '1'
    }
  ]
}
{
  id: '1',
  Fields: [
    {
      id: 'b12b4216-c190-404d-844b-47d31d7917e7',
      name: 'first', <-------------------------------------------------------------- !!! STILL ORIGINAL NAME
      projectId: '1'
    },
    {
      id: '004fe98e-c1e3-4c28-bf6b-fc76463c5257',
      name: 'secondXXX', <---------------------------------------------------------- UPDATED NAME
      projectId: '1'
    }
  ]
}
@ymc9
Copy link
Member

ymc9 commented Mar 8, 2024

Hi @DamianKu , thanks for reporting it. I'll look into it and let you know my findings.

@ymc9
Copy link
Member

ymc9 commented Mar 8, 2024

Hi @DamianKu , I've made a fix and pushed a new version 1.10.2. Could you update and see if it's working now? Thanks!

@damiankuriata
Copy link
Author

Hi @DamianKu , I've made a fix and pushed a new version 1.10.2. Could you update and see if it's working now? Thanks!

Hi @ymc9, it's working well 😃 Thanks 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants