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

loadManyToManyRelationIdsAndGroup returns undefined related for existing entities #7152

Open
2 of 22 tasks
Aleeexx opened this issue Dec 4, 2020 · 1 comment
Open
2 of 22 tasks

Comments

@Aleeexx
Copy link

Aleeexx commented Dec 4, 2020

Issue Description

Until yesterday all of our entities had an integer id. Now, some of them do have an UUID instead. So, their id property is of type string and saved in a BIN(16) column. In order to query an UUID entity, we need to transform the UUID string into a Buffer. Sadly TypeORM does not do that on querying relations (having a binary id) but we can work around that by, among other places, using a transformer in the entity.

@RelationId((table1Entity: Table1Entity) => table1Entity.table2Entity)
@Column({ transformer: new UUIDTransformer() })   // <-- Transform string to buffer and from buffer to string
public propertyName;

Since we do use a DataLoader, we use RelationIdLoader#loadManyToManyRelationIdsAndGroup to perform batch loading.

// If we have entityMetadatas, we can create the loaders for the typeorm relations
    if (connection.entityMetadatas) {
      // For every entity, we create an entry in the loaders
      connection.entityMetadatas.forEach(entityMetadata => {
        const resolverName = entityMetadata.targetName;
        if (!resolverName) return;

        // Add the loader for the entity
        if (!loaders[resolverName]) loaders[resolverName] = {};

        // For every relation, we create a dataLoader
        entityMetadata.relations.forEach(relation => {
          if (!loaders[resolverName][relation.propertyName]) {
            // Create the new loader
            loaders[resolverName][relation.propertyName] = new DataLoader((entities: any[]) => {
              return connection.relationIdLoader.loadManyToManyRelationIdsAndGroup(relation, entities).then(groups => groups.map(group => group.related));
            });
          }
        });
      });
    }

Expected Behavior

Using RelationIdLoader#loadManyToManyRelationIdsAndGroup should give us the related entities of the given UUID entities, as it still works for entities with an integer id (not needing a transformer).

Actual Behavior

The related property of RelationIdLoader#loadManyToManyRelationIdsAndGroup `s return value is undefined for every entity, since the transformer is not applied for the ids (see first comment in this issue).

const result = await this.relationIdLoader.loadManyToManyRelationIdsAndGroup(relation, entities);
console.log(result);
/*
[
  {
    entity: { id: <Buffer 10 eb 35 83 dd 7e 6c 50 87 5e 71 2c 19 bb a1 5a> },
    related: undefined,
  },
  ...
]
*/

My Environment

Dependency Version
Node.js version v10.20.1
Typescript version v3.8.3
TypeORM version v0.2.24

Relevant Database Driver(s)

  • MariaDB
  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

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, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.
@Aleeexx Aleeexx changed the title loadManyToManyRelationIdsAndGroup returns undefined related loadManyToManyRelationIdsAndGroup illogically returns empty related for entities with BIN primary key Dec 4, 2020
@Aleeexx Aleeexx changed the title loadManyToManyRelationIdsAndGroup illogically returns empty related for entities with BIN primary key loadManyToManyRelationIdsAndGroup illogically returns undefined related for entities with BIN primary key Dec 4, 2020
@Aleeexx Aleeexx changed the title loadManyToManyRelationIdsAndGroup illogically returns undefined related for entities with BIN primary key loadManyToManyRelationIdsAndGroup illogically returns undefined related for existing entities with BIN primary key Dec 6, 2020
@Aleeexx Aleeexx changed the title loadManyToManyRelationIdsAndGroup illogically returns undefined related for existing entities with BIN primary key loadManyToManyRelationIdsAndGroup returns undefined related for existing entities Dec 11, 2020
@Aleeexx
Copy link
Author

Aleeexx commented Dec 14, 2020

Further investigation showed, the specific load*() methods (like loadForOneToManyAndOneToOneNotOwner etc.) from the RelationIdLoader try to match the child entity with a parent entity by on the one hand the relationIds and on the other the relatedEntities, which are fetched by getRawMany().

The entries of the relationIds array do have still have the ID as a buffer (RowDataPacket)
but the IDs of the entries of the relatedEntities array were transformed back into an uuid (Instantiated entities).

So, the comparison will never successfully match for related Entities and therefore related will always be undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant