Skip to content

[Bug]: Typegen doesn't support id db fields without a default value #11961

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

Open
1 task done
Tobbe opened this issue Mar 1, 2025 · 3 comments
Open
1 task done

[Bug]: Typegen doesn't support id db fields without a default value #11961

Tobbe opened this issue Mar 1, 2025 · 3 comments
Assignees

Comments

@Tobbe
Copy link
Contributor

Tobbe commented Mar 1, 2025

What's not working?

This is the generated code:

export const createUnit: MutationResolvers['createUnit'] = ({ input }) => {
  return db.unit.create({
    data: input,
  })
}

data gets red squiggles.

Image

To dig a bit deeper in what's wrong you can do this to get better type info

type CreateUnitInput = Parameters<MutationResolvers['createUnit']>[0]['input']

export const createUnitCustom: MutationResolvers['createUnit'] = ({
  input,
}: {
  input: CreateUnitInput
}) => {

Image

What Prisma is expecting is this:

  /**
   * Unit create
   */
  export type UnitCreateArgs<ExtArgs extends $Extensions.InternalArgs = $Extensions.DefaultArgs> = {
    /**
     * Select specific fields to fetch from the Unit
     */
    select?: UnitSelect<ExtArgs> | null
    /**
     * Choose, which related nodes to fetch as well
     */
    include?: UnitInclude<ExtArgs> | null
    /**
     * The data needed to create a Unit.
     */
    data: XOR<UnitCreateInput, UnitUncheckedCreateInput>
  }

Especially data is interesting. It wants either UnitCreateInput xor UnitUncheckedCreateInput

And that's not what our generated types are.

How do we reproduce the bug?

I haven't created a minimal reproduction yet, but you at least need two prisma models where one model has a relation to the other. I don't know if there are more specific requirements than that to trigger the bug.

This might be enough of my schema to trigger the issue

model Unit {
  id           String        @id
  createdAt    DateTime      @default(now())
  updatedAt    DateTime      @updatedAt
  name         String
  restaurant   Restaurant    @relation(fields: [restaurantId], references: [id])
  restaurantId String
}

model Restaurant {
  id                String          @id @default(cuid())
  createdAt         DateTime        @default(now())
  updatedAt         DateTime        @updatedAt
  name              String
  units             Unit[]
}

What's your environment? (If it applies)

yarn rw info

  System:
    OS: macOS 15.2
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.14.0 - /private/var/folders/7j/9p56mwmj6jsfdr2h5_884j9w0000gn/T/xfs-75cb72c7/node
    Yarn: 4.4.0 - /private/var/folders/7j/9p56mwmj6jsfdr2h5_884j9w0000gn/T/xfs-75cb72c7/yarn
  Databases:
    SQLite: 3.43.2 - /usr/bin/sqlite3
  Browsers:
    Safari: 18.2
  npmPackages:
    @redwoodjs/auth-dbauth-setup: 8.2.0 => 8.2.0
    @redwoodjs/core: 8.2.0 => 8.2.0
    @redwoodjs/project-config: 8.2.0 => 8.2.0
  redwood.toml:
    [web]
      title = "Victory"
      port = 8910
      apiUrl = "/.redwood/functions"
      includeEnvironmentVariables = [
        # Add any ENV vars that should be available to the web side to this array
        # See https://redwoodjs.com/docs/environment-variables#web
      ]
    [api]
      port = 8911
    [browser]
      open = true
    [notifications]
      versionUpdates = ["latest"]
    [generate]
      tests = false
      stories = false

Are you interested in working on this?

  • I'm interested in working on this
@Tobbe Tobbe self-assigned this Mar 1, 2025
@Tobbe
Copy link
Contributor Author

Tobbe commented Mar 1, 2025

Looking closer at the type RW currently generates I see this

Image
scrolling down...
Image

So we have restaurantId: Scalars["String"];

That means we want Prisma's type to resolve to the UnitUncheckedCreateInput type but for some reason it's not doing that.
Haven't figured out why yet.

The type we're generating is missing id! Weird 🤔

Updating the destructured type to this gets rid of the red squiggles

type CreateUnitInput = Parameters<
  MutationResolvers['createUnit']
>[0]['input'] & { id: string }

@Tobbe
Copy link
Contributor Author

Tobbe commented Mar 1, 2025

I think I figured it out. I haven't verified this yet, but I guess we assume that id is autogenerated, and thus should not be supplied when doing "create". Or at least that it's optional. Which, if you look at my model, it isn't.

@Tobbe
Copy link
Contributor Author

Tobbe commented Mar 1, 2025

Confirmed that updating my prisma model to have this solved the red squiggle problem.

id           String        @id @default(uuid())

The issue is still valid though. We could do a better job generating our types to support ids with no default value

@Tobbe Tobbe changed the title [Bug]: Typegen: Generated service has errors for create resolver [Bug]: Typegen: Generated service has errors for create resolver when the id in the db model has no default value Mar 1, 2025
@Tobbe Tobbe changed the title [Bug]: Typegen: Generated service has errors for create resolver when the id in the db model has no default value [Bug]: Typegen doesn't support id db fields without a default value Mar 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant