question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to Define Bi-directional Relationships using TypeORM (Circular dependency)

See original GitHub issue

Expected Behavior

When using the Nest CLI, I can define bidirectional relationships such as:

menu.entity.ts

@ManyToOne((type) => Brand, brand=> brand.menus)
@JoinColumn()
brand: Brand;

brand.entity.ts

@OneToMany((type) => Menu, menu => menu.brand)
 menus: Menu;

Current Behavior

When you attempt this, you cannot serve the API and you get the errors outlined in Failure Information.

This works COMPLETELY in Nest CLI.

Failure Information (for bugs)

WARNING in Circular dependency detected:
apps/fourburner-api/src/resources/brands/models/brand.entity.ts -> apps/fourburner-api/src/resources/menus/models/menu.entity.ts -> apps/fourburner-api/src/resources/brands/models/brand.entity.ts

WARNING in Circular dependency detected:
apps/fourburner-api/src/resources/menu-item-modifiers/models/menu-item-modifier.entity.ts -> apps/fourburner-api/src/resources/menu-item-modifiers/models/menu-item-modifier.entity.ts

WARNING in Circular dependency detected:
apps/fourburner-api/src/resources/menus/models/menu.entity.ts -> apps/fourburner-api/src/resources/brands/models/brand.entity.ts -> apps/fourburner-api/src/resources/menus/models/menu.entity.ts

WARNING in Circular dependency detected:
apps/fourburner-api/src/resources/orders/models/order.entity.ts -> apps/fourburner-api/src/resources/orders/models/order.entity.ts

WARNING in Circular dependency detected:
apps/fourburner-api/src/resources/restaurants/models/restaurant.entity.ts -> apps/fourburner-api/src/resources/brands/models/brand.entity.ts -> apps/fourburner-api/src/resources/menus/models/menu.entity.ts -> apps/fourburner-api/src/resources/restaurants/models/restaurant.entity.ts

followed by:

ReferenceError: Cannot access 'Brand' before initialization
    at Module.Brand (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/main.js:1156:97)
    at Module../apps/fourburner-api/src/resources/menus/models/menu.entity.ts (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/webpack:/apps/fourburner-api/src/resources/menus/models/menu.entity.ts:49:10)
    at __webpack_require__ (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/webpack:/webpack/bootstrap:19:1)
    at Module../apps/fourburner-api/src/resources/brands/models/brand.entity.ts (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/main.js:1161:83)
    at __webpack_require__ (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/webpack:/webpack/bootstrap:19:1)
    at Module../apps/fourburner-api/src/resources/restaurants/models/restaurant.entity.ts (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/main.js:2664:85)
    at __webpack_require__ (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/webpack:/webpack/bootstrap:19:1)
    at Module../apps/fourburner-api/src/resources/assignedroles/models/assigned_role.entity.ts (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/main.js:979:95)
    at __webpack_require__ (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/webpack:/webpack/bootstrap:19:1)
    at Module../apps/fourburner-api/src/shared/auth/auth.service.ts (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/main.js:3864:110)

Steps to Reproduce

  1. Create an NX app with Nest.js
  2. Create two entities which have bi-directional relationships
  3. Serve the API using NX

Context

Please provide any relevant information about your setup:

NX Report output:

@nrwl/angular : 9.2.3
  @nrwl/cli : 9.2.3
  @nrwl/cypress : 9.2.3
  @nrwl/eslint-plugin-nx : Not Found
  @nrwl/express : Not Found
  @nrwl/jest : 9.2.3
  @nrwl/linter : 9.2.3
  @nrwl/nest : 9.2.3
  @nrwl/next : Not Found
  @nrwl/node : 9.2.3
  @nrwl/react : Not Found
  @nrwl/schematics : Not Found
  @nrwl/tao : 9.2.3
  @nrwl/web : Not Found
  @nrwl/workspace : 9.2.3
  typescript : 3.8.3

3rd party versions:

"typeorm": "^0.2.24",
"pg": "^8.0.3",

Failure Logs

Included above.

Other

I’ve found this exact bug mentioned across several other products that use NX, but for some reason no bug report here. This is a common thing it seems for NX + NestJS users.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:42
  • Comments:35 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
sinnrrrcommented, Feb 19, 2021

Any updates on the issue?

@mmcxii already solved the issue, the answer is to set "module": "commonjs" in the "compilerOptions" of tsconfig

8reactions
sinnrrrcommented, Dec 12, 2020

+1 just to keep this issue open

Read more comments on GitHub >

github_iconTop Results From Across the Web

Circular Dependency with Nestjs Swagger 4 - Stack Overflow
The solution that worked for me was to declare in @ApiProperty() the type with arrow function, like below: @Entity() export class Job {...
Read more >
Circular Dependency - A progressive Node.js framework
A circular dependency occurs when two classes depend on each other. For example, class A needs class B, and class B also needs...
Read more >
Types of relationships with Postgres & TypeORM. TypeScript ...
In this article, we cover creating relationships in Postgres with TypeORM, including One-To-One, One-To-Many with Many-To-One ...
Read more >
One-to-one relations - typeorm - GitBook
One-to-one is a relation where A contains only one instance of B, and B contains only one instance of A. Let's take for...
Read more >
Hibernate Tips: How to map a bidirectional many-to-many ...
The relationship definition consists of two mandatory and one optional part. ... You can therefore just reference the attribute on the Book entity...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found