zenstack icon indicating copy to clipboard operation
zenstack copied to clipboard

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

Open onimitch opened this issue 1 year ago • 0 comments

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

onimitch avatar Sep 27 '24 14:09 onimitch