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] Reference error when child models belong to a user #12

Open
OmegaHawkeye opened this issue Apr 30, 2024 · 0 comments
Open

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

OmegaHawkeye opened this issue Apr 30, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@OmegaHawkeye
Copy link
Owner

From kirimase created by kaikun213: nicoalbanese#172

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):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

@OmegaHawkeye OmegaHawkeye added the bug Something isn't working label Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

No branches or pull requests

1 participant