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

Type error on query with select field (although query runs successfully) #15615

Closed
marceloverdijk opened this issue Sep 29, 2022 · 4 comments · Fixed by #20309
Closed

Type error on query with select field (although query runs successfully) #15615

marceloverdijk opened this issue Sep 29, 2022 · 4 comments · Fixed by #20309
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: client types Types in Prisma Client topic: reserved words See https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#naming-conventions topic: type-clash
Milestone

Comments

@marceloverdijk
Copy link

Bug description

I'm having the following query:

const seasonDrivers = await prisma.seasonEntrantDriver.findMany({
  select: {
    driver: {
      select: {
        id: true,
        name: true,
        nationality: {
          select: {
            alpha2Code: true,
          },
        },
      },
    },
  },
  where: {
    year: currentSeasonYear,
  },
})

and although the query returns the expected results like:

[
  {
    "driver": {
      "id": "valtteri-bottas",
      "name": "Valtteri Bottas",
      "nationality": {
        "alpha2Code": "FI"
      }
    }
  },
  ..
]

I get a type error in Visual Studio Code on the top select part stating:

Type '{ driver: { select: { id: true; name: true; nationality: { select: { alpha2Code: true; }; }; }; }; }' is not assignable to type 'SeasonEntrantDriverSelect'.
  Types of property 'constructor' are incompatible.
    Type 'Function' is not assignable to type 'boolean | ConstructorArgs'.ts(2322)

which results in more type errors downstream as the seasonDrivers var cannot be typed anymore; which also means auto-completion is not working anymore.

Probably it is related to the constructor name used as field.
Note that however I can query my constructor table without any problems like
const constructors = await prisma.constructor.findMany({..})
so it is not a general problem to use the name constructor.

Even a similar query using a nested constructor field like below gives no type error at all:

const seasonConstructors = await prisma.seasonEntrantConstructor.findMany({
  select: {
    constructor: {
      select: {
        id: true,
        name: true,
        country: {
          select: {
            alpha2Code: true,
          },
        },
      },
    },
  },
  where: {
    year: currentSeasonYear,
  },
})

How to reproduce

.

Expected behavior

No response

Prisma information

Here is a part of the Prisma schema.

If needed I can share the full schema.

model Season {
  year                                   Int                                     @map("year") @id
  seasonEntrants                         SeasonEntrant[]                         @relation("SeasonEntrantsBySeason")
  seasonEntrantConstructors              SeasonEntrantConstructor[]              @relation("SeasonEntrantConstructorsBySeason")
  seasonEntrantTyreManufacturers         SeasonEntrantTyreManufacturer[]         @relation("SeasonEntrantTyreManufacturersBySeason")
  seasonEntrantDrivers                   SeasonEntrantDriver[]                   @relation("SeasonEntrantDriversBySeason")
  @@map("season")
}

model SeasonEntrant {
  year                                   Int                                     @map("year")
  season                                 Season                                  @relation(name: "SeasonEntrantsBySeason", fields: [year], references: [year])
  entrantId                              String                                  @map("entrant_id")
  entrant                                Entrant                                 @relation(name: "SeasonEntrantsByEntrant", fields: [entrantId], references: [id])
  countryId                              String                                  @map("country_id")
  country                                Country                                 @relation(name: "SeasonEntrantsByCountry", fields: [countryId], references: [id])
  seasonEntrantConstructors              SeasonEntrantConstructor[]              @relation("SeasonEntrantConstructorsBySeasonEntrant")
  seasonEntrantTyreManufacturers         SeasonEntrantTyreManufacturer[]         @relation("SeasonEntrantTyreManufacturerBySeasonEntrant")
  seasonEntrantDrivers                   SeasonEntrantDriver[]                   @relation("SeasonEntrantDriversBySeasonEntrant")
  @@map("season_entrant")
  @@id([year, entrantId])
}

model SeasonEntrantConstructor {
  year                                   Int                                     @map("year")
  season                                 Season                                  @relation(name: "SeasonEntrantConstructorsBySeason", fields: [year], references: [year])
  entrantId                              String                                  @map("entrant_id")
  entrant                                Entrant                                 @relation(name: "SeasonEntrantConstructorsByEntrant", fields: [entrantId], references: [id])
  seasonEntrant                          SeasonEntrant                           @relation(name: "SeasonEntrantConstructorsBySeasonEntrant", fields: [year, entrantId], references: [year, entrantId])
  constructorId                          String                                  @map("constructor_id")
  constructor                            Constructor                             @relation(name: "SeasonEntrantConstructorsByConstructor", fields: [constructorId], references: [id])
  engineManufacturerId                   String                                  @map("engine_manufacturer_id")
  engineManufacturer                     EngineManufacturer                      @relation(name: "SeasonEntrantConstructorsByEngineManufacturer", fields: [engineManufacturerId], references: [id])
  seasonEntrantTyreManufacturers         SeasonEntrantTyreManufacturer[]         @relation("SeasonEntrantTyreManufacturersBySeasonEntrantConstructor")
  seasonEntrantDrivers                   SeasonEntrantDriver[]                   @relation("SeasonEntrantDriversBySeasonEntrantConstructor")
  @@map("season_entrant_constructor")
  @@id([year, entrantId, constructorId, engineManufacturerId])
}

model SeasonEntrantTyreManufacturer {
  year                                   Int                                     @map("year")
  season                                 Season                                  @relation(name: "SeasonEntrantTyreManufacturersBySeason", fields: [year], references: [year])
  entrantId                              String                                  @map("entrant_id")
  entrant                                Entrant                                 @relation(name: "SeasonEntrantTyreManufacturersByEntrant", fields: [entrantId], references: [id])
  seasonEntrant                          SeasonEntrant                           @relation(name: "SeasonEntrantTyreManufacturerBySeasonEntrant", fields: [year, entrantId], references: [year, entrantId])
  constructorId                          String                                  @map("constructor_id")
  constructor                            Constructor                             @relation(name: "SeasonEntrantTyreManufacturersByConstructor", fields: [constructorId], references: [id])
  engineManufacturerId                   String                                  @map("engine_manufacturer_id")
  engineManufacturer                     EngineManufacturer                      @relation(name: "SeasonEntrantTyreManufacturersByEngineManufacturer", fields: [engineManufacturerId], references: [id])
  seasonEntrantConstructor               SeasonEntrantConstructor                @relation(name: "SeasonEntrantTyreManufacturersBySeasonEntrantConstructor", fields: [year, entrantId, constructorId, engineManufacturerId], references: [year, entrantId, constructorId, engineManufacturerId])
  tyreManufacturerId                     String                                  @map("tyre_manufacturer_id")
  tyreManufacturer                       TyreManufacturer                        @relation(name: "SeasonEntrantTyreManufacturersByTyreManufacturer", fields: [tyreManufacturerId], references: [id])
  @@map("season_entrant_tyre_manufacturer")
  @@id([year, entrantId, constructorId, engineManufacturerId, tyreManufacturerId])
}

model SeasonEntrantDriver {
  year                                   Int                                     @map("year")
  season                                 Season                                  @relation(name: "SeasonEntrantDriversBySeason", fields: [year], references: [year])
  entrantId                              String                                  @map("entrant_id")
  entrant                                Entrant                                 @relation(name: "SeasonEntrantDriversByEntrant", fields: [entrantId], references: [id])
  seasonEntrant                          SeasonEntrant                           @relation(name: "SeasonEntrantDriversBySeasonEntrant", fields: [year, entrantId], references: [year, entrantId])
  constructorId                          String                                  @map("constructor_id")
  constructor                            Constructor                             @relation("SeasonEntrantDriversByConstructor", fields: [constructorId], references: [id])
  engineManufacturerId                   String                                  @map("engine_manufacturer_id")
  engineManufacturer                     EngineManufacturer                      @relation(name: "SeasonEntrantDriversByEngineManufacturer", fields: [engineManufacturerId], references: [id])
  seasonEntrantConstructor               SeasonEntrantConstructor                @relation(name: "SeasonEntrantDriversBySeasonEntrantConstructor", fields: [year, entrantId, constructorId, engineManufacturerId], references: [year, entrantId, constructorId, engineManufacturerId])
  driverId                               String                                  @map("driver_id")
  driver                                 Driver                                  @relation(name: "SeasonEntrantDriversByDriver", fields: [driverId], references: [id])
  @@map("season_entrant_driver")
  @@id([year, entrantId, constructorId, engineManufacturerId, driverId])
}

Environment & setup

  • OS: macOS
  • Database: SQLite
  • Node.js version: v18.4.0

Prisma Version

prisma                  : 4.3.1
@prisma/client          : 4.3.1
Current platform        : darwin
Query Engine (Node-API) : libquery-engine c875e43600dfe042452e0b868f7a48b817b9640b (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine        : migration-engine-cli c875e43600dfe042452e0b868f7a48b817b9640b (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine    : introspection-core c875e43600dfe042452e0b868f7a48b817b9640b (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary           : prisma-fmt c875e43600dfe042452e0b868f7a48b817b9640b (at node_modules/@prisma/engines/prisma-fmt-darwin)
Format Wasm             : @prisma/prisma-fmt-wasm 4.3.0-32.c875e43600dfe042452e0b868f7a48b817b9640b
Default Engines Hash    : c875e43600dfe042452e0b868f7a48b817b9640b
Studio                  : 0.473.0
@marceloverdijk marceloverdijk added the kind/bug A reported bug. label Sep 29, 2022
@millsp millsp added team/client Issue for team Client. bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. topic: client types Types in Prisma Client labels Sep 30, 2022
@janpio janpio added the topic: reserved words See https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#naming-conventions label Oct 13, 2022
@marceloverdijk
Copy link
Author

Hi @janpio thanks for adding this topic to the issue.
Looking ahead is this something Prisma will fix one day, or not (so better that I use an alternative name)

@marceloverdijk
Copy link
Author

Just for reference , as per the example above I also have Constructor model.

To be able to query it, I had to do this as well:

prisma.constructor = prisma.Constructor

Not proud of it but it worked to be able to do prisma.constructor.findMany({}) etc.

@janpio
Copy link
Member

janpio commented Nov 2, 2022

We will definitely fix this - but more "one day" than "tomorrow" as using another name is a simple enough workaround for now. Sorry about that.

@marceloverdijk
Copy link
Author

@janpio yes I changed all constructor fields in my schema to konstructor ...
but of course I would love to change it back "one day" ;-)

@Jolg42 Jolg42 added this to the 5.1.0 milestone Jul 20, 2023
@Jolg42 Jolg42 added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Jul 20, 2023
SevInf added a commit that referenced this issue Jul 20, 2023
* fix(client): Allow to use `constructor` as a model name

Fix #15615

* Update snapshots

* Remove debugging leftovers

* Fix enums
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: client types Types in Prisma Client topic: reserved words See https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#naming-conventions topic: type-clash
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants