Skip to content
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

Merged
merged 2 commits into from
Oct 21, 2020
Merged

Conversation

ilyasfoo
Copy link
Contributor

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.
@imnotjames
Copy link
Contributor

This needs a test to confirm the be behavior.

@ilyasfoo
Copy link
Contributor Author

I've added the tests

Copy link
Contributor

@imnotjames imnotjames left a 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.

@ilyasfoo
Copy link
Contributor Author

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]));
Copy link
Member

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...

Copy link
Contributor

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.

zaro pushed a commit to zaro/typeorm that referenced this pull request Jan 12, 2021
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest-accepted label hacktoberfest
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants