nicoalbanese / kirimase

Build full-stack Next.js apps, incredibly fast
https://kirimase.dev
MIT License
2.57k stars 118 forks source link

[BUG] Reference error when child models belong to a user #172

Open kaikun213 opened 6 months ago

kaikun213 commented 6 months ago

Config File

{
  "hasSrc": true,
  "packages": [
    "shadcn-ui",
    "prisma",
    "next-auth"
  ],
  "preferredPackageManager": "bun",
  "t3": false,
  "alias": "@",
  "analytics": true,
  "rootPath": "src/",
  "componentLib": "shadcn-ui",
  "orm": "prisma",
  "driver": "pg",
  "auth": "next-auth"
}

Describe the bug If I select the option "Does this model belong to the user? yes" for child models, it will mess up the references due to the fields becoming DocumentType instead of documentTypes

To Reproduce

? Please select the resources you would like to generate: Model, Controller, View
? Please select the type of view you would like to generate: Server Actions with Optimistic UI
? Please select any additional controllers you would like to generate:
? Please enter the table name (plural and in snake_case): organisations
? Please select the type of this field: string
? Please enter the field name (in snake_case): name
? Is this field required? yes
? Would you like to add another field? no
? Would you like to set up an index? no
? Would you like timestamps (createdAt, updatedAt)? yes
? Does this model belong to the user? yes
? Would you like to add a child model? (organisations) yes
? Please enter the table name (plural and in snake_case): document_types
? Please select the type of this field: string
? Please enter the field name (in snake_case): name
? Is this field required? yes
? Would you like to add another field? no
? Would you like to set up an index? yes
? Which field would you like to index? name
? Would you like timestamps (createdAt, updatedAt)? yes
? Does this model belong to the user? yes
? Would you like to add a child model? (document_types) no
? Would you like to add a child model? (organisations) yes
? Please enter the table name (plural and in snake_case): documents
? Please select the type of this field: string
? Please enter the field name (in snake_case): title
? Is this field required? yes
? Would you like to add another field? no
? Would you like to set up an index? yes
? Which field would you like to index? title
? Would you like timestamps (createdAt, updatedAt)? yes
? Does this model belong to the user? yes
? Would you like to add a child model? (documents) no
? Would you like to add a child model? (organisations) no
? Would you like to add a link to 'Organisations' in your sidebar? yes
ℹ Updated Prisma schema                                                                                   10:38:45 AM
? Would you like to add a link to 'Document Types' in your sidebar? yes
ℹ Updated Prisma schema                                                                                   10:38:47 AM
? Would you like to add a link to 'Documents' in your sidebar? yes
ℹ Updated Prisma schema                                                                                   10:38:48 AM

Results in

model Organisation {
  id           String         @id @default(cuid())
  name         String
  userId       String
  user         User           @relation(fields: [userId], references: [id], onDelete: Cascade)
  createdAt    DateTime       @default(now())
  updatedAt    DateTime       @updatedAt
  DocumentType DocumentType[]
  Document     Document[]
}

Which for example gives an error in lib/api/organisations/queries.ts

export const getOrganisationByIdWithDocumentTypesAndDocuments = async (id: OrganisationId) => {
  const { session } = await getUserAuth();
  const { id: organisationId } = organisationIdSchema.parse({ id });
  const o = await db.organisation.findFirst({
    where: { id: organisationId, userId: session?.user.id!},
    include: { documentTypes: { include: {organisation: true } }, documents: { include: {organisation: true } } }
  });
  if (o === null) return { organisation: null };
  const { documentTypes, documents, ...organisation } = o;

  return { organisation, documentTypes:documentTypes, documents:documents };
};

The error:

Object literal may only specify known properties, but 'documentTypes' does not exist in type 'OrganisationInclude<DefaultArgs>'. Did you mean to write 'DocumentType'?ts(2561)

Expected behavior If I generate without the "belongs to user" option for child models, it works well.

Screenshots

Desktop (please complete the following information):

Additional context Add any other context about the problem here.