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

generated schemas issue #2

Closed
brainwind-software opened this issue Jun 2, 2022 · 4 comments
Closed

generated schemas issue #2

brainwind-software opened this issue Jun 2, 2022 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@brainwind-software
Copy link

brainwind-software commented Jun 2, 2022

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
@brainwind-software
Copy link
Author

brainwind-software commented Jun 2, 2022

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

@omar-dulaimi omar-dulaimi self-assigned this Jun 3, 2022
@omar-dulaimi omar-dulaimi added the bug Something isn't working label Jun 3, 2022
@omar-dulaimi
Copy link
Owner

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.

@omar-dulaimi omar-dulaimi moved this from To do to In progress in Issues Progress Tracking Jun 3, 2022
@omar-dulaimi omar-dulaimi changed the title Implicit many-to-many relations not working !? generated schemas issue Jun 3, 2022
@rrmckinley
Copy link

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.

@omar-dulaimi
Copy link
Owner

@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 :)

Issues Progress Tracking automation moved this from In progress to Done Jun 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
No open projects
Development

No branches or pull requests

3 participants