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.

Type error on 1-1 relationships

See original GitHub issue

I have the given (simplified) schema where User and Keychain have a 1-1 relationship:

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URI")
}

generator client {
  provider        = "prisma-client-js"
}

generator zod {
  provider      = "zod-prisma"
  output        = "./schemas"
  relationModel = true
}

model User {
  id       String    @id @default(uuid())
  keychain Keychain?
}

model Keychain {
  userID String @id
  owner  User   @relation(fields: [userID], references: [id])
}

Those relationships have to be defined as nullable on one end, but the generated user schema generates a TypeScript error:

import * as z from "zod"
import { CompleteKeychain, relatedKeychainSchema } from "./index"

export const userSchema = z.object({
  id: z.string()
})

export interface CompleteUser extends z.infer<typeof userSchema> {
  keychain: CompleteKeychain | null
}

/**
 * relatedUserSchema contains all relations on your model in addition to the scalars
 *
 * NOTE: Lazy required in case of potential circular dependencies within schema
 */
export const relatedUserSchema: z.ZodSchema<CompleteUser> = z.lazy(() => userSchema.extend({
  keychain: relatedKeychainSchema.nullish(), // <= ts2322
}))

Removing the .nullish() modifier for the keychain in relatedUserSchema fixes the issue.

Edit: using .nullable() there works too, which was what was used before.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
CarterGrimmeisencommented, Jan 20, 2022

0.5.2 has been published with a fix for this.

1reaction
fergusmeiklejohncommented, Jan 20, 2022

I’ve got this too. Replacing .nullish() with .nullable() in the related Models fixed the typescript error. It looks like its because it could be undefined Screenshot 2022-01-20 at 10 10 15 AM

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type I & Type II Errors | Differences, Examples, Visualizations
In statistics, a Type I error is a false positive conclusion, while a Type II error is a false negative conclusion.
Read more >
Type II Error - Definition, How to Avoid, and Example
The type II error is also known as a false negative. The type II error has an inverse relationship with the power of...
Read more >
What are Type I and Type II Errors? - Simply Psychology
A type II error is also known as a false negative and occurs when a researcher fails to reject a null hypothesis which...
Read more >
Can anyone help me figure out why my relationships are ...
I'm getting an error message that says "relationships must be on the same number of fields with the same data types". I can't...
Read more >
Explain the relationship between TYPE I error and Type II error.
A type I error covers those errors where the null hypothesis, which accurately describes the data, is rejected. This complements the type II...
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