Prisma include becomes undefined when importing nested include objects across multiple files
Problem:
I'm working with Prisma and TypeScript and I have a set of nested include objects split across multiple files.
However, when I import and use them in a repository, some of the nested includes appear as undefined at runtime.
When I log the include objects, I get:
And Prisma only returns:
The nested
city -> department -> region relations are not loaded because the include was undefined.
Expected behaviour:
ownerIndexInclude should contain all nested includes, and Prisma should hydrate the entire object tree.
Question:
Is this a known issue with Prisma + TypeScript module evaluation order? How can I reliably build nested include objects across multiple files without them becoming undefined? Any help or recommended pattern would be appreciated.1 Reply
You selected the bot's instantaneous insights. Let me compile your answer right away!
This issue is not specific to Prisma itself, but rather a common JavaScript/TypeScript pitfall related to circular dependencies and module evaluation order. When you split deeply nested include objects across multiple files and import them into each other, you can easily create a circular dependency. This causes some imported values to be undefined at runtime, which is exactly what you’re seeing: nested include objects are undefined, so Prisma cannot hydrate the full relation tree.
Evidence from Knowledge Sources:
- There is no explicit mention of this exact problem in the knowledge sources, but similar issues with type inference and includes are discussed. For example, users report that when using include objects from variables, type inference or runtime behavior can differ from inline definitions, but the root cause in your case is the undefined value, not Prisma’s type system or runtime itself [Github Issue #19125].
How to Fix:
- Avoid circular imports. Instead, define your nested include objects in a single file, or build them up in a way that does not require cross-file references.
- If you must split them, use functions to construct the include objects at runtime, which can help avoid circular references.
- Alternatively, use Prisma’s validator API to define includes in a type-safe way, but this does not solve the circular import issue itself [Prisma Validator Discussion].
Example Fix:
If you see undefined, you likely have a circular import. Refactor to avoid this pattern.
*
*Summary:**
This is a module system/circular dependency issue, not a Prisma bug. Refactor your includes to avoid circular imports, or build them in a single file or via functions. The knowledge sources do not mention a Prisma-specific workaround for this scenario. ✅ Great, thanks for letting us know that this solved your question!
This is a module system/circular dependency issue, not a Prisma bug. Refactor your includes to avoid circular imports, or build them in a single file or via functions. The knowledge sources do not mention a Prisma-specific workaround for this scenario. ✅ Great, thanks for letting us know that this solved your question!