Skip to content

Foreign key is incorrect set with default value in a nested creation #1997

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

Closed
ymc9 opened this issue Feb 21, 2025 · 0 comments
Closed

Foreign key is incorrect set with default value in a nested creation #1997

ymc9 opened this issue Feb 21, 2025 · 0 comments

Comments

@ymc9
Copy link
Member

ymc9 commented Feb 21, 2025

ZModel:

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

    users         User[]
    posts         Post[]
    comments      Comment[]
    postUserLikes PostUserLikes[]
}

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 Post {
    tenantId String          @default(auth().tenantId)
    tenant   Tenant          @relation(fields: [tenantId], references: [id])
    id       String          @default(uuid())
    author   User            @relation(fields: [authorId], references: [id])
    authorId String          @default(auth().id)

    comments Comment[]
    likes    PostUserLikes[]

    @@id([tenantId, id])

    @@allow('all', true)
}

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

    userId   String
    user     User   @relation(fields: [userId], references: [id])

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

    @@id([tenantId, id])
    @@unique([tenantId, userId, postId])

    @@allow('all', true)
}

model Comment {
    tenantId String @default(auth().tenantId)
    tenant   Tenant @relation(fields: [tenantId], references: [id])
    id       String @default(uuid())
    postId   String
    post     Post   @relation(fields: [tenantId, postId], references: [tenantId, id])

    @@id([tenantId, 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(prisma, { user: { id: user.id, tenantId: tenant.id } });

        await db.post.create({
            data: {
                likes: {
                    createMany: {
                        data: [{
                            userId: user.id
                        }]
                    }
                }
            },
            include: {
                likes: true
            }
        });

Error:
// Prisma error: Unknown argument `tenantId`. Available options are marked with ?.]

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

1 participant