omar-dulaimi / prisma-trpc-generator

Prisma 2+ generator to emit fully implemented tRPC routers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

generated schemas issue

brainwind-software opened this issue · comments

Bug description

Hi,

I have a problem getting implicit many-to-many relations to work with the generated code.
Attached you find my schema and client-code.

Problem is, VSCode and Angular Live Deployment Server both show the same TypeScript error, related to the generated type:

error TS2739: Type '{ create: { id: number; name: string; }[]; }' is missing the following properties from type '{ connect: { id: number; } | { id: number; }[]; create: { name: string; } | { name: string; id: number; } | { name: string; }[] | { name: string; id: number; }[]; connectOrCreate: { create: { name: string; } | { ...; }; where: { ...; }; } | { ...; }[]; }': connect, connectOrCreate

53             tags: {
               ~~~~

  ./prisma/trpc/schemas/objects/ProjectUncheckedCreateWithoutAuthorInput.schema.ts:12:3
    12   tags: z.object(TagUncheckedCreateNestedManyWithoutProjectsInputSchemaObject),
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The expected type comes from property 'tags' which is declared here on type '{ id: number; tags: { connect: { id: number; } | { id: number; }[]; create: { name: string; } | { name: string; id: number; } | { name: string; }[] | { name: string; id: number; }[]; connectOrCreate: { create: { name: string; } | { ...; }; where: { ...; }; } | { ...; }[]; }; ... 4 more ...; desc: string; }'

So the type needs all of create, connect & createOnConnect methods ?!

Or am I doing something else wrong ?
With plain prisma the same query is working perfectly.

How to reproduce

use my schema, the generator & my client code

Expected behavior

Type should work with create method only

Prisma information

schema:

model User {
  id        Int       @id @default(autoincrement())
  createdAt DateTime  @default(now())
  email     String    @unique
  name      String?
  password  String?
  projects  Project[]
}

model Project {
  id        Int              @id @default(autoincrement())
  createdAt DateTime         @default(now())
  published Boolean          @default(false)
  title     String
  shortDesc String
  desc      String
  author    User             @relation(fields: [authorId], references: [id])
  authorId  Int
  tags      Tag[]
}

model Tag {
  id       Int              @id @default(autoincrement())
  name     String
  projects Project[]
}

client-code:

trcp.mutation('user.createOneUser', {
  data: {
    email: 'tester@brainwind.de',
    name: 'Fabian Dreßen',
    projects: {
      create: [{
        title: 'Sample project  ',
        shortDesc: 'BlaBla Blub',
        desc: 'adasdasd dasdasdasd',
        tags: {
          create: [
            { name: 'sometag' },
            { name: 'anothertag' },
          ],
        }
      }],
    }
  }
});

Environment & setup

  • OS: Ubuntu
  • Database: SQLite
  • Node.js version: v14.18.1

Prisma Version

prisma                  : 3.14.0
@prisma/client          : 3.14.0
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine 2b0c12756921c891fec4f68d9444e18c7d5d4a6a (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli 2b0c12756921c891fec4f68d9444e18c7d5d4a6a (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core 2b0c12756921c891fec4f68d9444e18c7d5d4a6a (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt 2b0c12756921c891fec4f68d9444e18c7d5d4a6a (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : 2b0c12756921c891fec4f68d9444e18c7d5d4a6a
Studio                  : 0.460.0

Further more, I just found out,
with my schema, I cannot create a tag, without related projects:

trpc.mutation('tag.createOneTag', {
  data: {
    name: 'sometag' ,
  }
});

Tells me:

Property 'projects' is missing in type '{ name: string; }'

But in pure Prisma its working fine:

prisma.tag.create({
    data: {
         name: 'sometag' ,
    }
})

I guess the zod-schema is missing the .optional() chain

Hey @brainwind-software

Thanks for the report. It definitely seems like a bug in the generated schemas.
Under the hood it uses prisma-zod-generator.

Working on it now.

Thanks for the report. This is affecting me too. Object maybe undefined on input errors on mutations in the generated routers.

@omar-dulaimi thanks for the project

Hey @brainwind-software

Thanks for the report.

@brainwind-software @rrmckinley

Should be fixed with 0.1.10.

This was a very stubborn bug.

Please make sure to star the repo if you like this project. Thank you :)