Two entities pointing to the same entity using the same property name will make Typeorm mix them up
See original GitHub issueIssue 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:
- Created 2 years ago
- Reactions:10
- Comments:19 (10 by maintainers)
Top GitHub Comments
Fix will be released on a next release (I plan to do it next week)
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.