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.

JOIN with no results produces entity with nulled properties

See original GitHub issue

Issue type:

[x] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

First, thank you for providing this amazing ORM to the community. You guys are doing awesome work on this project.

Now, I have an issue with “custom” many-to-many that you hopefully can help me with.

I wanted to have a Many-to-many relationship setup using TypeORM, but I needed to add some extra properties to the pivot table between them and was therefore not able to use an “automatically” generated pivot table.

Instead, I ended up doing this:

@Entity()
export class User {

    ...other properties

    @OneToMany(type => Participant, participants => participants.user)
    events: Array<Participant>;

}

@Entity()
export class Participant {

    ...other properties

    @ManyToOne(type => CalendarEvent, calendarEvent => 
    calendarEvent.participants, { primary: true })
    event: CalendarEvent;

    @ManyToOne(type => User, user => user.events, { primary: true })
    user: User;

}

@Entity()
export class CalendarEvent {

    ...other properties

    @OneToMany(type => Participant, participants => participants.event)
    participants: Array<Participant>;

}

This works if I left JOIN from either CalendarEvent to pivot, or from User to pivot and will produce an empty array if there are no results. But if I left join once more to get the other entity as well, I get a pivot object back where all properties are null.

Joining once:

return this.entityManager.createQueryBuilder(User, "user")
                                .leftJoinAndSelect("user.events", "pivot")
                                .where("user.id = :ID", {
                                    ID: "UUID-in-here"
                                })
                                .getOne();

It produces empty arrays when there is no results:

{
    "id": "UUID-in-here"
    "events": []
}

Joining again to retreive the event data:

return this.entityManager.createQueryBuilder(User, "user")
                                .leftJoinAndSelect("user.events", "pivot")
                                .leftJoinAndSelect("pivot.event", "event")
                                .where("user.id = :ID", {
                                    ID: "UUID-in-here"
                                })
                                .getOne();

I get the following result:

{
    "id": "UUID-in-here"
    "events": [
        {
            ...extra pivot columns here with value = null
            "event": null
        }
    ]
}

This doesn’t happen if you left JOIN multiple times using “normal” many-to-many relationships. For instance, if I run this:

return this.entityManager.createQueryBuilder(User, "user")
                            .leftJoinAndSelect("user.teams", "team")
                            .leftJoinAndSelect("team.messages", "message")
                            .where("user.id = :ID", {
                                ID: "UUID-in-here"
                            })
                            .getOne();

Where user is many-to-many with teams, and team is many-to-many with messages, I get empty arrays if there is no relationship.

Either:

{
    "id": "UUID-in-here",
    "teams": []
}

or:

{
    "id": "UUID-in-here",
    "teams": [
        "id": "UUID-in-here",
        "messages": []
    ]
}

How do I setup the relationship/query to avoid having that object with nulled properties when there are no results?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
CarstenWalthercommented, Jun 19, 2019

I stumbled across the same problem yesterday. It has something to do with the composed primary key over two relations (over event and user in the example above). If there is a separate primary id column instead, the result does not contain the object with null properties.

1reaction
MardariGcommented, Dec 16, 2020

Omg, this issue is from before COVID era. Any milestone?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using IS NULL or IS NOT NULL on join conditions - Theory ...
Yes, it will! Because a RIGHT JOIN will show all results that match (the first INNER JOIN we did) plus all rows from...
Read more >
How to SELECT Records With No NULL Values in MySQL
Use the MySQL engine to only grab records that you desire while excluding those with pesky NULL columns with the IS NOT NULL...
Read more >
What Makes a Contract Null and Void? These Mistakes Do.
When a contract is void, it's unenforceable because it's missing one of the required elements of a legal agreement. And there's no point...
Read more >
How to join unrelated entities with JPA and Hibernate
You can also use the relationship attributes in JPQL queries to join related entities. The trouble starts as soon as you want to...
Read more >
free ssn dob - La Via Lattea Food
Fill the empty fields; involved parties names, addresses and numbers etc. ... Free ssn dob - Sell Company Fullz with With a 1-inch...
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