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

Joins in select_related ignoring nulls, should maybe use isouter? #45

Open
joshsharp opened this issue Jul 26, 2019 · 0 comments
Open

Comments

@joshsharp
Copy link

Hello! I've run into an issue using select_related. I've noticed that where a relation is null and the related table is listed in select_related, there is no record retrieved for the original model. Using these models for example:

class Track(orm.Model):
    # fields omitted, not relevant  

class Post(orm.Model):
    # snip...
    track = orm.ForeignKey(Track, allow_null=True)

Let's say I have two posts with IDs 1 and 2. Post 2 does not have a related track. If I write the query Post.objects.select_related('track').all(), I only get post 1 returned.

However, if the select_from in build_select_expression is modified to include isouter:

select_from = sqlalchemy.sql.join(select_from, model_cls.__table__, isouter=True)

This seems to do the trick.

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

No branches or pull requests

1 participant