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

[BUG] Polymorphic CreateInput/UpdateInput type doesn't contain relation Id fields, or requires delegate field to be specified #1749

Closed
onimitch opened this issue Sep 27, 2024 · 2 comments
Milestone

Comments

@onimitch
Copy link

Description and expected behavior

Schema

model Asset {
    @@map('assets')

    id          String       @id @default(cuid())
    createdAt   DateTime     @default(now())
    updatedAt   DateTime     @default(now()) @updatedAt

    url         String
    downloadUrl String
    pathname    String       @omit
    size        BigInt

    createdBy   User?    @relation(fields: [createdById], references: [id], onUpdate: Restrict, onDelete: SetNull)
    createdById String?      @default(auth().id)

    show        Show?        @relation(fields: [showId], references: [id], onUpdate: Restrict, onDelete: SetNull)
    showId      String?

    org         Organisation @relation(fields: [orgId], references: [id], onUpdate: Restrict, onDelete: Restrict)
    orgId       String       @default(auth().orgId)

    contentType String
    @@delegate(contentType)
}

model Image extends Asset {
    @@map('images')
}

model Sound extends Asset {
    @@map('sounds')
}

the image.create method's data param, only accepts these types:

export type ImageCreateInput = {
    createdAt?: Date | string;
    updatedAt?: Date | string;
    id?: string;
    url: string;
    downloadUrl: string;
    pathname: string;
    size: bigint | number;
    createdBy?: UserCreateNestedOneWithoutDelegate_aux_User_assets_ImageInput;
    show?: ShowCreateNestedOneWithoutDelegate_aux_Show_assets_ImageInput;
    org?: OrganisationCreateNestedOneWithoutDelegate_aux_Organisation_assets_ImageInput;
};
export type ImageUncheckedCreateInput = {
    createdAt?: Date | string;
    updatedAt?: Date | string;
    id?: string;
    url: string;
    downloadUrl: string;
    pathname: string;
    size: bigint | number;
    createdById?: string | null;
    showId?: string | null;
    orgId?: string;
    contentType: string;
};

The following create call has a TS error because data is missing contentType. Looking at the above types it's clear why, if I specify createdById, showId or orgId, then the type can only be ImageUncheckedCreateInput.

const result = await db.image.create({
    data: {
        url: blob.url,
        downloadUrl: blob.downloadUrl,
        pathname: blob.pathname,
        size: await size,
        createdById: payload.userId,
        showId: payload.showId,
        orgId: payload.orgId,
    },
});

Just to clarify, if I run this code and ignore the TS error, it works fine. So this is just an incorrect type.

Also minor note, but I noticed as seen above that the delegate field contentType is simply of type string. It would be nice, if possible of course, if that type could actually be more specific. In my case it would be 'Image' | 'Sound'

Environment (please complete the following information):

  • ZenStack version: 2.6.1
  • Prisma version: 5.20.0
  • Database type: Postgresql
@ymc9 ymc9 added this to the v2.8.0 milestone Oct 21, 2024
@ymc9
Copy link
Member

ymc9 commented Nov 1, 2024

Fixed by #1804 in v2.7.4

@ymc9 ymc9 closed this as completed Nov 1, 2024
@onimitch
Copy link
Author

onimitch commented Nov 6, 2024

Confirmed fix on my end (I tested with 2.8.0), no more type errors. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants