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

[Bug] Nested create does not set default values for delegate fields #2019

Open
Gabrola opened this issue Feb 27, 2025 · 7 comments
Open

[Bug] Nested create does not set default values for delegate fields #2019

Gabrola opened this issue Feb 27, 2025 · 7 comments

Comments

@Gabrola
Copy link
Contributor

Gabrola commented Feb 27, 2025

ZModel:

model Tenant {
  id      String    @id @default(uuid())

  users   User[]
  content Content[]
}

model User {
  id       String          @id @default(uuid())
  tenantId String          @default(auth().tenantId)
  tenant   Tenant          @relation(fields: [tenantId], references: [id])
  posts    Post[]
  likes    PostUserLikes[]

  @@allow('all', true)
}

model Content {
  tenantId    String @default(auth().tenantId)
  tenant      Tenant @relation(fields: [tenantId], references: [id])
  id          String @id @default(uuid())
  contentType String

  @@delegate(contentType)
  @@allow('all', true)
}

model Post extends Content {
  author   User            @relation(fields: [authorId], references: [id])
  authorId String          @default(auth().id)

  comments Comment[]
  likes    PostUserLikes[]

  @@allow('all', true)
}

model PostUserLikes extends Content {
  userId String
  user   User   @relation(fields: [userId], references: [id])

  postId String
  post   Post   @relation(fields: [postId], references: [id])

  @@unique([userId, postId])

  @@allow('all', true)
}

model Comment extends Content {
  postId String
  post   Post   @relation(fields: [postId], references: [id])

  @@allow('all', true)
}

TS:

const tenant = await prisma.tenant.create({ data: {} });
const user = await prisma.user.create({ data: { tenantId: tenant.id } });
const db = enhance({ id: user.id, tenantId: tenant.id });
await db.post.create({
    data: {
        likes: {
            createMany: {
                data: [
                    {
                        userId: user.id,
                    },
                ],
            },
        },
    },
    include: {
        likes: true,
    },
});

Error:

const result = await db[model].create({
  data: {
    likes: {
      create: [
        {
          delegate_aux_content: {
            create: {
              contentType: "PostUserLikes"
            }
          },
          user: {
            connect: {
              id: "a6b6af76-0320-4761-ba41-e78b8a05ccf9"
            }
          }
        }
      ]
    },
    author: {
      connect: {
        id: "a6b6af76-0320-4761-ba41-e78b8a05ccf9"
      }
    },
    delegate_aux_content: {
      create: {
        contentType: "Post",
        tenant: {
          connect: {
            id: "0bb02fe8-c823-4a3a-8843-2068c6072971"
          }
        }
      }
    }
  },
  select: {
    id: true,
    likes: {
      select: {
        id: true
      }
    }
  }
})

Argument `tenant` is missing.

ZenStack version: 2.12.0

@Gabrola
Copy link
Contributor Author

Gabrola commented Feb 27, 2025

Looks like it was broken by #1989 when trying to fix #1997

@Gabrola
Copy link
Contributor Author

Gabrola commented Feb 27, 2025

Commenting out this line

fixes it but breaks #1997 again

@ymc9
Copy link
Member

ymc9 commented Feb 28, 2025

Thanks @Gabrola , I'll look into it too. And your other PRs 😄

ymc9 added a commit that referenced this issue Feb 28, 2025
@Gabrola
Copy link
Contributor Author

Gabrola commented Mar 1, 2025

Thank you @ymc9 ! Great work as always! 🙌

@ymc9
Copy link
Member

ymc9 commented Mar 1, 2025

Thank you @ymc9 ! Great work as always! 🙌

It turns out your original fix is (almost) correct 😄, and mine has issues. I thought about it again and relying on the backlink information from the context is sufficient. Making the updated fix now!

@ymc9
Copy link
Member

ymc9 commented Mar 1, 2025

Also I saw a new sponsorship from @suhyl-hq, I believe it's either you made it directly or pushed for it. Thanks a lot for the generosity. It means a lot to the project! @Gabrola

@Gabrola
Copy link
Contributor Author

Gabrola commented Mar 1, 2025

@ymc9 yes, that's us :) We were in the middle of a major schema refactor and discovered ZenStack, and it has very cleanly solved so many of the problems we either already hack-ily solved or still needed to solve! So lots of love and support for this project is more than warranted 🙌

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