typegraphql-prisma icon indicating copy to clipboard operation
typegraphql-prisma copied to clipboard

Empty Input Type for many to many relationship join tables

Open rossinicolas opened this issue 5 years ago • 20 comments
trafficstars

After excecute npx prisma generate we have some Input Types that's are empty, so, when i tried to run the nest app i have the following Error:

[Nest] 4976   - 2020-10-16 12:59:05   [ExceptionHandler] Some errors occurred while generating GraphQL schema:
  Input Object type Sgp_ch_turnos_agenteUpdateManyDataInput must define one or more fields.

that's my auto generated file:

import * as TypeGraphQL from "type-graphql";
import GraphQLJSON from "graphql-type-json";
import { JsonValue, InputJsonValue } from "@prisma/client";

@TypeGraphQL.InputType({
  isAbstract: true,
  description: undefined,
})
export class Sgp_ch_turnos_agenteUpdateManyDataInput {
}

rossinicolas avatar Oct 16 '20 16:10 rossinicolas

Looks like your model does not have any updateble fields, only pks and fks 😕 Can you share the Sgp_ch_turnos_agente model from prisma schema?

BTW, you should map the table name to more readable model name 😝

MichalLytek avatar Oct 16 '20 16:10 MichalLytek

Hi @MichalLytek , first of all, the table name are comming from an old database that're using for a legacy system in my company ;) LOL

that's the data table model generated by introspection

model sgp_ch_turnos_agente {
  idturno       Int           @default(1)
  idagente      Int
  sgp_agente    sgp_agente    @relation(fields: [idagente], references: [idagente])
  sgp_ch_turnos sgp_ch_turnos @relation(fields: [idturno], references: [idturno])

  @@id([idturno, idagente])
}

Thanks in advance for your assistance

rossinicolas avatar Oct 16 '20 16:10 rossinicolas

it's a many-to-many relationship between sgp_agante and sgp_ch_turno datatables without aditional fields

image

rossinicolas avatar Oct 19 '20 10:10 rossinicolas

@rossinicolas Could you also raise the issue in Prisma repo?

It looks like the schema introspection should detect your case and model it as a many-to-many relation in models, without the need of sgp_ch_turnos_agente in the middle 🤔

MichalLytek avatar Oct 20 '20 19:10 MichalLytek

@rossinicolas Tracked by issue on Prisma repo: https://github.com/prisma/prisma/issues/4004

It has to be fixed upstream. For now you need to manually modify the generated files to fix schema errors.

MichalLytek avatar Oct 30 '20 13:10 MichalLytek

Thanks.

El El vie, 30 oct. 2020 a la(s) 10:33, Michał Lytek < [email protected]> escribió:

@rossinicolas https://github.com/rossinicolas Tracked by issue on Prisma repo: prisma/prisma#4004 https://github.com/prisma/prisma/issues/4004

It has to be fixed upstream. For now you need to manually modify the generated files to fix schema errors.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/MichalLytek/typegraphql-prisma/issues/19#issuecomment-719554702, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHMAEEITLOZFABYOMIB6WF3SNK6DBANCNFSM4STQTDIQ .

-- Nicolás Rossi [email protected]

rossinicolas avatar Oct 30 '20 14:10 rossinicolas

Hello, I'm also getting this error and I understand that it's because my model doesn't have any updateable fields?

model RelNode {
    id Int @id @default(autoincrement())

    linkedBy     RelNode[]      @relation("NodeLinks", references: [id])
    linking      RelNode[]      @relation("NodeLinks", references: [id])
    TextFragment TextFragment[]
}

In that case wouldn't it be better and more user-friendly to omit the empty type?

iSplasher avatar Mar 09 '21 22:03 iSplasher

If I omit the type, then I have to propagate that change up in the input types change to exclude a field of that type, etc.

So it's a too complex workaround, for now please bump issue on Prisma to get more attention: https://github.com/prisma/prisma/issues/4004

MichalLytek avatar Mar 10 '21 10:03 MichalLytek

Hello @MichalLytek, would it be possible to rename this issue to something like "Many to many relationship: join tables causes empty input type" or something like that? it would be easier to spot and we would avoid duplicating the bug 😄

ZobairQ avatar Mar 11 '21 07:03 ZobairQ

As a workaround I solved this by adding this to my schema

createdAt   DateTime @default(now())

not a solution, but workaround to skip the error

kkaustubhvyas avatar Aug 29 '21 07:08 kkaustubhvyas

Is there any updates on? Edit: Seems like prisma did resolve their issue

ZobairQ avatar Jan 26 '22 09:01 ZobairQ

@ZobairQ No, the Prisma issue is still open: https://github.com/prisma/prisma/issues/4004

MichalLytek avatar Jan 26 '22 09:01 MichalLytek

Hey @MichalLytek, any updates on this? The prisma issue is closed. I also get these errors with e.g. the following model:

model TemplateResource {
  templateVerId    Int
  resourceId       Int
  templateVersion  TemplateVersion    @relation(fields: [templateVerId], references: [id])
  resource         Resource           @relation(fields: [resourceId], references: [id])

  @@id([templateVerId, resourceId])
}

Error: Input Object type TemplateResourceUpdateManyMutationInput must define one or more fields.

itpropro avatar Nov 15 '22 15:11 itpropro

Any update? I am still getting errors related to this issue

DanielRios549 avatar Nov 30 '22 12:11 DanielRios549

@MichalLytek sir, any updates on the issue or any work around for this, cause we're still getting the error in my case it's saying the field is undefined

image

nikhilswain avatar Feb 10 '23 22:02 nikhilswain

This issue was quite a roadblock for a project at my job. We have this database that contains a few join tables with no additional fields, causing the Input Object type ... must define one or more fields. error. Modifying the Prisma schema was unfortunately not an option because we cannot add columns to the database itself. Instead, I figured I could just add a dummy field to the generated TypeGraphQL input type.

After all, if you go deep enough down the rabbit hole, you'll see that it's all caused by an (arguably arbitrary and artificial) constraint in the GraphQL specification. There was interest in changing the spec (https://github.com/graphql/graphql-spec/pull/606) but, for some reason, it all died out in the end. The current state is that some GraphQL implementations do force input object types to have at a field, and others simply don't care. Among those that do follow the spec more closely, https://github.com/graphql/graphql-js is the one causing the issue here.

Going back to the workaround, I've forked a new @ono-lean-logistics/typegraphql-prisma NPM package which merely adds a dummy _ nullable int field whenever a type doesn't have any field already (which should only occur in *UpdateManyMutationInput types).

Because this is a very quick n' dirty solution, I'm not 100% sure that it deserves a PR. But it's also true that join tables are an essential component of relational databases and one shouldn't encounter an issue such as this to begin with. @MichalLytek What do you think?

eliaperantoni avatar May 29 '23 09:05 eliaperantoni

Also, I think the "blocked" label should be removed now, as the Prisma issue that was marked as the culprit is now closed.

eliaperantoni avatar May 29 '23 15:05 eliaperantoni

This is a serious blocker in a project I work on.



Solutions like
 createdAt DateTime @default(now())
 doesn’t seem to be working.



My coworker found that setting useUncheckedScalarInputs to true 

solves this issue. 

 

With that option set to true I see that there're 2 inputs generated

  • one empty, the same that was generated before which was causing the issues but now it's not used
  • a new one that is used instead of the previous one 

 

 According to Prisma UncheckedInput documentation it's recommended to use "safe" input types. However after reading that piece of documentation it looks like unchecked inputs aren't really less safe than normal inputs.

@MichalLytek any thoughts on this solution?

MrDonArmando avatar Aug 10 '23 11:08 MrDonArmando

I guess we should focus on a solution which removes the empty input type (and related mutations?) from the GraphQL schema. As Michal already noticed in the linked Prisma issue, an update operation without any data is senseless. I will invest some time here on the weekend.

dahaupt avatar Feb 16 '24 14:02 dahaupt