diff --git a/packages/client/tests/functional/issues/16196-upsert-nested-select/_matrix.ts b/packages/client/tests/functional/issues/16196-upsert-nested-select/_matrix.ts new file mode 100644 index 000000000000..58ad17761d3d --- /dev/null +++ b/packages/client/tests/functional/issues/16196-upsert-nested-select/_matrix.ts @@ -0,0 +1,15 @@ +import { defineMatrix } from '../../_utils/defineMatrix' + +export default defineMatrix(() => [ + [ + { + provider: 'sqlite', + }, + { + provider: 'postgresql', + }, + { + provider: 'cockroachdb', + }, + ], +]) diff --git a/packages/client/tests/functional/issues/16196-upsert-nested-select/prisma/_schema.ts b/packages/client/tests/functional/issues/16196-upsert-nested-select/prisma/_schema.ts new file mode 100644 index 000000000000..462e7e5cd59e --- /dev/null +++ b/packages/client/tests/functional/issues/16196-upsert-nested-select/prisma/_schema.ts @@ -0,0 +1,36 @@ +import { idForProvider } from '../../../_utils/idForProvider' +import testMatrix from '../_matrix' + +export default testMatrix.setupSchema(({ provider }) => { + return /* Prisma */ ` + generator client { + provider = "prisma-client-js" + } + + datasource db { + provider = "${provider}" + url = env("DATABASE_URI_${provider}") + } + + model User { + id ${idForProvider(provider)} + email String @unique + posts Post[] + profile Profile? + } + + model Post { + id ${idForProvider(provider)} + title String + user User @relation(fields: [userId], references: [id]) + userId String + } + + model Profile { + id ${idForProvider(provider)} + name String + user User @relation(fields: [userId], references: [id]) + userId String @unique + } + ` +}) diff --git a/packages/client/tests/functional/issues/16196-upsert-nested-select/tests.ts b/packages/client/tests/functional/issues/16196-upsert-nested-select/tests.ts new file mode 100644 index 000000000000..5f08cc6bec32 --- /dev/null +++ b/packages/client/tests/functional/issues/16196-upsert-nested-select/tests.ts @@ -0,0 +1,131 @@ +import { randomBytes } from 'crypto' + +import testMatrix from './_matrix' +// @ts-ignore +import type { PrismaClient } from './node_modules/@prisma/client' + +declare let prisma: PrismaClient + +const id = randomBytes(12).toString('hex') +testMatrix.setupTestSuite( + (suiteConfig, suiteMeta) => { + beforeAll(async () => { + await prisma.user.create({ + data: { + id: id, + email: 'john@doe.io', + posts: { + create: [{ title: 'A' }, { title: 'B' }, { title: 'C' }], + }, + }, + }) + }) + + test('upsert (update) with nested select many relation', async () => { + const data = await prisma.user.upsert({ + where: { id }, + create: { + email: 'john@doe.io', + }, + update: { + email: 'johnny@doe.io', + }, + select: { + posts: { + select: { + user: { + select: { + email: true, + }, + }, + }, + }, + }, + }) + + expect(data).toMatchInlineSnapshot(` + Object { + posts: Array [ + Object { + user: Object { + email: johnny@doe.io, + }, + }, + Object { + user: Object { + email: johnny@doe.io, + }, + }, + Object { + user: Object { + email: johnny@doe.io, + }, + }, + ], + } + `) + }) + + test('upsert (update) with nested select one relation', async () => { + const data = await prisma.user.upsert({ + where: { id }, + create: { + email: 'john@doe.io', + }, + update: { + email: 'johnny@doe.io', + }, + select: { + profile: { + select: { + user: { + select: { + email: true, + }, + }, + }, + }, + }, + }) + + expect(data).toMatchInlineSnapshot(` + Object { + profile: null, + } + `) + }) + + test('upsert (create) with nested select one relation', async () => { + const data = await prisma.user.upsert({ + where: { id: randomBytes(12).toString('hex') }, + create: { + email: 'jane@joe.io', + }, + update: { + email: 'janette@doe.io', + }, + select: { + email: true, + profile: { + select: { + id: true, + }, + }, + }, + }) + + expect(data).toMatchInlineSnapshot(` + Object { + email: jane@joe.io, + profile: null, + } + `) + }) + }, + { + optOut: { + from: ['mysql', 'mongodb', 'sqlserver'], + reason: "We don't support native upserts for these", + }, + }, +)