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.

Two entities pointing to the same entity using the same property name will make Typeorm mix them up

See original GitHub issue

Issue Description

Two entities pointing (many to one) to the same entity using the same property name will make Typeorm mix them up, if the other entity is pointing back to one of them (one to many).

This appears to be a regression, since it worked on 0.2.32 but not on 0.2.33 and the latest 0.2.36

Expected Behavior

I set up a few entities like this:

Process --(N:1)-> Stage --(1:N)-> StageType ProcessTemplate --(N:1)-> ProcessTemplateStage --(1:N)-> StageType

where StageType is the same entity.

When doing a query like this:

const processTemplates = await connection.manager
  .getRepository(ProcessTemplate)
  .find({
    relations: ["stages", "stages.stageType"],
  });

console.log(processTemplates[0].stages);

The resulting log in 0.2.32 and earlier is:

[
  ProcessTemplateStage {
    id: 1,
    name: 'some prt stage',
    templateStageSpecificProp: 'some value here',
    stageType: null
  },
  ProcessTemplateStage {
    id: 2,
    name: 'some prt stage 2',
    templateStageSpecificProp: 'text',
    stageType: null
  },
  ProcessTemplateStage {
    id: 3,
    name: 'some prt stage 3',
    templateStageSpecificProp: 'text',
    stageType: null
  }
]

Actual Behavior

In 0.2.33 and later the same code gives this output:

[
  ProcessTemplateStage {
    id: 1,
    name: 'some prt stage',
    templateStageSpecificProp: 'some value here',
    stageType: null
  },
  Stage { name: 'some prt stage 2', stageType: null },
  Stage { name: 'some prt stage 3', stageType: null }
]

The first object is correct, the two others are interpreted as Stage entity and only have properties that match exactly between Stage and ProcessTemplateStage.

The query going to postgres is same on both versions, so the problem appears to be in the parsing of the db result.

Steps to Reproduce

I set up an example repository to reproduce the problem. https://github.com/hehall/typeorm-issue

In the StageType entity I’m pointing back to Stage. When I remove this line everything works as expected.

  @OneToMany((type) => Stage, (stage) => stage.stageType)
  stages: Stage[];

The problem won’t appear if I’m just getting all ProcessTemplateStages joining in the StageTypes. The issue is only there when I join all the way from the ProcessTemplate.

My Environment

Dependency Version
Operating System Windows
Node.js version 14.16.1
Typescript version 3.3.3333
TypeORM version 0.2.33 - 0.2.36

Relevant Database Driver(s)

Only tested with postgres

DB Type Reproducible
aurora-data-api no
aurora-data-api-pg no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres yes
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, and I know how to start.
  • ✅ Yes, I have the time, but I don’t know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:10
  • Comments:19 (10 by maintainers)

github_iconTop GitHub Comments

8reactions
pleerockcommented, Oct 28, 2021

Fix will be released on a next release (I plan to do it next week)

7reactions
hehallcommented, Aug 5, 2021

Added a test case for this.

I think the issue itself is too hard for myself to fix on my own though.

Looking through the changes to from 0.2.32 to 0.2.33, I found this PR https://github.com/typeorm/typeorm/pull/3160, which might have something to do with this. But it’s just a guess.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Inheritance - typeorm - GitBook
Single table inheritance is a pattern when you have multiple classes with their own properties, but in the database they are stored in...
Read more >
TypeORM Relations Tutorial - FULL details! - YouTube
In this video we're going to go deep into the fundamentals of creating relationships between tables in an application which uses TypeORM.
Read more >
TypeORM - Quick Guide - Tutorialspoint
ORM is a type of tool that maps entities with database tables. ... In many cases, the query to find different entity object...
Read more >
NestJS + TypeORM: Use two or more databases?
I just tried setting up TypeORM with multiple databases and a ormconfig.json and it did not work for me at all. It seemed...
Read more >
Is it good practice to use entity objects as data transfer objects?
Some people like mixing up entities and DTOs, some people don't. ... json at the same place (even if you can separate them...
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