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

Including Parent Entity with Child Entity #655

Closed
serkanz opened this issue Jul 13, 2020 · 5 comments
Closed

Including Parent Entity with Child Entity #655

serkanz opened this issue Jul 13, 2020 · 5 comments
Labels
bug Something isn't working

Comments

@serkanz
Copy link

serkanz commented Jul 13, 2020

I have 2 entity with bidirectional relationship (One Uygulama with Many Modul and Many Modul to One Uygulama).

I have a UygulamaId foreign key in my Modul table in my database which I join two tables.

Entity Uygulama

@Entity({ tableName: "Uygulamalar", collection: "Uygulamalar" })
export class Uygulama {
  @PrimaryKey({ fieldName: "Id" })
  Id!: number;
  @Property({ fieldName: "Adi" })
  Adi!: string;
  @Property({ fieldName: "Kod" })
  Kod!: string;
  @Property({ fieldName: "UygulamaSahibi" })
  UygulamaSahibi!: string;
  @Property({ fieldName: "createdAt" })
  CreatedAt = new Date();

  @Property({ fieldName: "updatedAt", onUpdate: () => new Date() })
  UpdatedAt = new Date();

  @OneToMany({ entity: () => Modul, mappedBy: "Uygulama", orphanRemoval: true })
  Moduller = new Collection<Modul>(this);
}

Entity Modul

@Entity({ tableName: "Moduller", collection: "Moduller" })
export class Modul {
  @PrimaryKey({ fieldName: "Id" })
  Id!: number;
  @Property({ fieldName: "Adi" })
  Adi!: string;
  @Property({ fieldName: "Kod" })
  Kod!: string;
  @Property({ fieldName: "createdAt" })
  CreatedAt = new Date();
  @Property({ fieldName: "updatedAt", onUpdate: () => new Date() })
  UpdatedAt = new Date();
  @ManyToOne({
    entity: () => Uygulama,
    joinColumn: "UygulamaId",
    inversedBy: "Moduller"
  })
  Uygulama!: Uygulama;
  @OneToMany({ entity: () => Ekran, mappedBy: "Modul", orphanRemoval: true })
  Ekranlar = new Collection<Ekran>(this);
}

When I try to return an Uygulama with all Moduls, I successfully return it with:

DI.uygulamaRepository.findOne({ Id: parseInt(id) }, ["Moduller"]);

However when I want to return a Modul with its parent Uygulama:

DI.modulRepository.findAll(["Uygulama"]);

returns :

(node:22100) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'name' of undefined
    at QueryBuilder.autoJoinPivotTable (d:\NODEJS\backend\node_modules\mikro-orm\dist\query\QueryBuilder.js:338:62)
    at d:\NODEJS\backend\node_modules\mikro-orm\dist\query\QueryBuilder.js:323:22
    at Array.forEach (<anonymous>)
    at QueryBuilder.finalize (d:\NODEJS\backend\node_modules\mikro-orm\dist\query\QueryBuilder.js:316:24)
    at QueryBuilder.getKnexQuery (d:\NODEJS\backend\node_modules\mikro-orm\dist\query\QueryBuilder.js:149:14)
    at QueryBuilder.execute (d:\NODEJS\backend\node_modules\mikro-orm\dist\query\QueryBuilder.js:185:72)
    at PostgreSqlDriver.find (d:\NODEJS\backend\node_modules\mikro-orm\dist\drivers\AbstractSqlDriver.js:27:19)
    at EntityManager.find (d:\NODEJS\backend\node_modules\mikro-orm\dist\EntityManager.js:76:43)
    at EntityRepository.findAll (d:\NODEJS\backend\node_modules\mikro-orm\dist\entity\EntityRepository.js:33:24)
    at d:\NODEJS\backend\dist\routers\Modul.js:19:54
    at Generator.next (<anonymous>)
    at d:\NODEJS\backend\dist\routers\Modul.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (d:\NODEJS\backend\dist\routers\Modul.js:4:12)
    at d:\NODEJS\backend\dist\routers\Modul.js:18:31
    at Layer.handle [as handle_request] (d:\NODEJS\backend\node_modules\express\lib\router\layer.js:95:5)

What am I missing here?

Kind regards

@B4nan
Copy link
Member

B4nan commented Jul 13, 2020

Hmmm looks like you ran into an edge case caused by using nonstandard property names (usually entity name is with first letter uppercase, while property names are with first letter lowercase) - the ORM sees a property that has a name Uygulama that is also a name of other entity - and it considers it to be an internal pivot table entity instead (which is nonsense, as you don't even have a M:N relation in the example).

One workaround could be to change the property name to something else.

@B4nan B4nan added the bug Something isn't working label Jul 13, 2020
@serkanz
Copy link
Author

serkanz commented Jul 13, 2020

Thanks @B4nan , I'll update property names and try again, and let you know about the results. How long would it take to fix the bug ?

@B4nan
Copy link
Member

B4nan commented Jul 13, 2020

Should be quite easy, but I would rather fix it in v4 as rebasing v4 with changes done in master is the painful part here...

@serkanz
Copy link
Author

serkanz commented Jul 14, 2020

Hi @B4nan , as you mentioned changing property name solved it. When a bugfix is released, I'll be glad to test :)

Hmmm looks like you ran into an edge case caused by using nonstandard property names (usually entity name is with first letter uppercase, while property names are with first letter lowercase) - the ORM sees a property that has a name Uygulama that is also a name of other entity - and it considers it to be an internal pivot table entity instead (which is nonsense, as you don't even have a M:N relation in the example).

One workaround could be to change the property name to something else.

B4nan added a commit that referenced this issue Jul 20, 2020
Previously `QueryBuilder` was blindly expecting that any populate request for a property
that name collides with an entity name means we want to do M:N population.

This fixes the check by verifying the `meta.pivotTable` flag.

Also new method on `MetadataStorage` that returns possibly undefined is added to improve
typings in internals.

Closes #655
@B4nan
Copy link
Member

B4nan commented Jul 20, 2020

Closing as fixed in v4, will be part of alpha 6.

@B4nan B4nan closed this as completed Jul 20, 2020
B4nan added a commit that referenced this issue Aug 2, 2020
Previously `QueryBuilder` was blindly expecting that any populate request for a property
that name collides with an entity name means we want to do M:N population.

This fixes the check by verifying the `meta.pivotTable` flag.

Also new method on `MetadataStorage` that returns possibly undefined is added to improve
typings in internals.

Closes #655
B4nan added a commit that referenced this issue Aug 9, 2020
Previously `QueryBuilder` was blindly expecting that any populate request for a property
that name collides with an entity name means we want to do M:N population.

This fixes the check by verifying the `meta.pivotTable` flag.

Also new method on `MetadataStorage` that returns possibly undefined is added to improve
typings in internals.

Closes #655
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants