-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
fix: TreeRepository based entities primary column supports custom name. #6942
Conversation
Previously "id" was hardcoded, thus errors when an entity has a custom primary column name other than "id". Now it fetches the column name from metadata instead.
This needs a test to confirm the be behavior. |
I've added the tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd normally be iffy that we're making assumptions about primary columns. However, these assumptions are all over the place in this repository - so.. well.. so be it. We need to come back and do another pass sometime to clean that up all over here.
I'd agree. I believe this fix is a step in the right direction since it also follows the existing assumptions. Refactoring in the future would be a little bit easier. |
@@ -265,7 +265,7 @@ export class TreeRepository<Entity> extends Repository<Entity> { | |||
const parentEntityId = this.metadata.primaryColumns[0].getEntityValue(entity); | |||
const childRelationMaps = relationMaps.filter(relationMap => relationMap.parentId === parentEntityId); | |||
const childIds = new Set(childRelationMaps.map(relationMap => relationMap.id)); | |||
entity[childProperty] = entities.filter(entity => childIds.has(entity.id)); | |||
entity[childProperty] = entities.filter(entity => childIds.has(entity[this.metadata.primaryColumns[0].propertyName])); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
relying on a single primary column always means there would be issues with multiple primary columns...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hundred percent agree.
The reason why I ended up accepting this is that there's several other places in the same file where this pattern is used - using the first primary column only - without even checking for its existence.
If we were to support multiple primary keys here it would have meant refactoring how other parts of the Tree Repository works, and balloon the scope of the change dramatically.
…e. (typeorm#6942) Previously "id" was hardcoded, thus errors when an entity has a custom primary column name other than "id". Now it fetches the column name from metadata instead.
Previously "id" was hardcoded, thus errors when an entity has a custom primary column name other than "id".
Now it fetches the column name from metadata instead.