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

LEFT JOIN doesn't work as expected for inherited entites when foreign key is None #182

Vitalium opened this issue May 20, 2016 · 1 comment


Copy link

@Vitalium Vitalium commented May 20, 2016


For example we have some inherited entities:

class User(db.Entity):
    name = Required(str)
    servers = Set("Server")

class Worker(db.User):

class Admin(db.Worker):

# And M:1 relationship with another entity
class Server(db.Entity):
    name = Required(str)
    user = Optional(User)

We can add 3 sample servers (one of them without user) and try to get server's and user's names:

    Server(name='', user=User(name="Alex"))
    Server(name='', user=Worker(name="John"))
    Server(name='', user=None)

    left_join((, for s in Server).show()

We get output:        |

There is missing|None

because we have such SQL-query:

SELECT DISTINCT "s"."name", "user-1"."name"
FROM "Server" "s"
  LEFT JOIN "User" "user-1"
    ON "s"."user" = "user-1"."id"
WHERE "user-1"."classtype" IN ('Admin', 'Worker', 'User')

To get full list with None users we need to move WHERE clause into LEFT JOIN, like this:

SELECT DISTINCT "s"."name", "user-1"."name"
FROM "Server" "s"
  LEFT JOIN "User" "user-1"
    ON "s"."user" = "user-1"."id" AND "user-1"."classtype" IN ('Admin', 'Worker', 'User')

Is there a workaround to get behavior like in last SQL?

@kozlovsky kozlovsky added the bug label May 20, 2016
@kozlovsky kozlovsky added this to the 0.6.6 milestone May 20, 2016
@kozlovsky kozlovsky self-assigned this May 20, 2016
Copy link

@kozlovsky kozlovsky commented May 20, 2016

Thanks for reporting! This is the bug and I'll fix it.

@kozlovsky kozlovsky closed this in 3f46189 Jun 21, 2016
kozlovsky added a commit that referenced this issue Aug 22, 2016
# New features

* Added native JSON data type support in all supported databases:

# Backward incompatible changes

* Dropped Python 2.6 support

# Improvements

* #179 Added the compatibility with PYPY using psycopg2cffi
* Added an experimental @db_session `strict` parameter:

# Bugfixes

* #182 - LEFT JOIN doesn't work as expected for inherited entities when foreign key is None
* Some small bugs were fixed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants