question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model named "Key" is incorrectly typed

See original GitHub issue

Bug description

I have a model called Key. When I run prisma generate and try to use the TypeScript types, the type for Key is incorrect. Looking at the generated TS file, I see two Key types.

I don’t see these in the reserved word list: https://github.com/prisma/prisma/blob/master/src/packages/client/src/generation/generateClient.ts#L368

How to reproduce

  1. Setup Prisma with the supplied schema.
  2. Look at the TS types
  3. Notice the TS type for the Key model is incorrect.

Expected behavior

There should not be a TS error.

If that’s not possible, maybe throw an error when using reserved words like Key or count.

Prisma information

model Key {
  id               Int                @id
  name             String?            @db.VarChar(45)
  AnotherTable     AnotherTable[]
}

model AnotherTable {
  id                     Int              @id @default(autoincrement())
  keyId                  Int
  Key                    Key              @relation(fields: [keyId], references: [id])

  @@index([keyId], name: "fk_AnotherTable_Key_idx")
}

When I run prisma generate and try to use the TypeScript types, the type for Key is incorrect.

type TResponse = Prisma.AnotherTableGetPayload<{
    include: {
        Key: true;
    };
}>

const obj: TResponse = {
    id: 4,
    Key: { id: 4, name: 'name' }
};

When I mouse over Key, I get the following:

(property) Key: Prisma.Key
Type '{ id: number; name: string; }' is not assignable to type 'Key'.
  Type '{ id: number; name: string; }' is not assignable to type 'symbol'.ts(2322)

Looking at the generated TS file, I see two Key types.

...

/**
 * Model Key
 */

export type Key = {
  id: number
  name: string | null
}

...

export namespace Prisma {

  ...

  type Key = string | number | symbol;

  ...

}

Below has been fixed by 2.23.0. Incidentally, as I was writing this description, I updated the Prisma version to the latest and ran into another similar error with a column named count. The generated TS file looked like this:

  export type ExerciseGroupByOutputType = {
    id: number
    count: number | null
    count: ExerciseCountAggregateOutputType | null
    avg: ExerciseAvgAggregateOutputType | null
    sum: ExerciseSumAggregateOutputType | null
    min: ExerciseMinAggregateOutputType | null
    max: ExerciseMaxAggregateOutputType | null
  }

I got the following errors:

error TS2300: Duplicate identifier 'count'.
error TS2717: Subsequent property declarations must have the same type.  Property 'count' must be of type 'number | null', but here has type 'ExerciseCountAggregateOutputType | null'.

Environment & setup

  • OS: Mac OS
  • Database: MySQL
  • Node.js version: v15.10.0

Prisma Version

2.22.1

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
pantharshit00commented, May 23, 2021

I can confirm this Key issue.

0reactions
janpiocommented, Dec 16, 2022

Yes, you are looking at the issue tracking it. Compared to other problems this is not important enough to be prioritized right now though. Sorry.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mysql Foreign key constraint is incorrectly formed error
My problem ended up being that the foreign key column and the referencing column were not of the same type or length. The...
Read more >
MariaDB: InnoDB foreign key constraint errors
Both messages are first referring to an internal table name and the foreign key error message is referring to an incorrect name.
Read more >
mariadb - "Foreign key constraint is incorrectly formed"
Note that I have a UNIQUE named CONSTRAINT on working_day - this is what should be done and is part of the SQL...
Read more >
How to Fix Keyboard Typing Wrong Character or Keys Not ...
If still problem not fixed contact me on fb...https://www.facebook.com/mjtube11Keyboard codes...
Read more >
3 common foreign key mistakes (and how to avoid them)
1. Mismatched data types. When you're using a foreign key to reference a column in another table, the datatypes of both tables have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found