question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Collection ordered wrong on 1:N relationship with eager loading

See original GitHub issue

Issue type: [X] question [?] bug report

Database system/driver: [X] mysql / mariadb

TypeORM version: [X] 0.2.6 (or put your version here)

I got a little problem working with typeorm. Now I am trying to find a record using findOne(entityId) method.

My table have 1:N relationship. For example,

class User {
    @PrimaryColumn()
    id: string

    @OneToMany(() => UserSignInLog, (userSignInLog: UserSignInLog) => userSignInLog.user, {
        cascade: true,
        eager: true    
    })
    signInLogs: UserSignInLog[]
}

class UserSignInLog {
    @PrimaryColumn({ })
    id: string

    @ManyToOne(() => User, (user: User) => user.signInLogs, {
      onDelete: 'CASCADE'
    })
    @JoinColumn({ name: 'userId'})
    user: User;
}

- Consider entity specification can be quite wrong because of sample.

The problem is, when I load user and all of the its signIn logs, that the SignInlogs are not ordered correctly as I inserted.

I read all of query logs of my database, and tried to execute same queries which are logged. The query results are not ordered correctly same as the results of findOne method. Some results are ordered fine, the others are ordered wrong: like, ‘B’ shows up after ‘A’ when it should be ‘A’ after ‘B’.

So, My question is,

  • Is there any options to set or change order when loading all collection using eager loading?

If not, in my humble opinion, it is not a code-level problem, so I hope you to check the your query builder when findOne method executes. All data recorded as I wanted, only findOne returns wrong result.

Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mirupalcommented, Jul 18, 2019

This was exactly what I was searching the docs for. Seemed logical to me to have an order option for relations.

Then I’ll have to use the querybuilder…

1reaction
pleerockcommented, Jun 4, 2018

Then the query builder has also no problem as you mentioned.

but in query builder you are explicit and you can set any order you want.

Here we can add another option like eagerOrder, but Im not sure if I want to add another complexity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wrong limit results on eager loading with order on secondary ...
The reason is PostgreSQL forbids queries with DISTINCT and ordering on columns not included in SELECT clause: SELECT DISTINCT posts.id FROM ...
Read more >
Eloquent eager load Order by - laravel - Stack Overflow
When you eager load you can't use an ORDER BY on the loaded relations because those will be requested and assembled as a...
Read more >
[Eloquent] Order by on an eager loaded relationship's foreign ...
The key here is to understand that when eager-loading Laravel executes a different query for each relationship. If you want a parent model...
Read more >
Relationship Loading Techniques
Eager loading refers to objects returned from a query with the related collection or scalar reference already loaded up front. The Query ...
Read more >
Relationship Loading Techniques
The scalar or collection attribute associated with a relationship() contains a ... In addition to “joined eager loading”, a second option for eager...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found