Skip to content

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

Closed
@ymc9

Description

@ymc9

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 ?.]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions