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

Closed
Vitalium opened this issue May 20, 2016 · 1 comment
Assignees
Labels
Milestone

Comments

@Vitalium
Copy link

Hello!

For example we have some inherited entities:

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

class Worker(db.User):
    pass

class Admin(db.Worker):
    pass

# 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='s1.example.com', user=User(name="Alex"))
    Server(name='s2.example.com', user=Worker(name="John"))
    Server(name='free.example.com', user=None)

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

We get output:

s.name        |s.user.name
--------------+-----------
s1.example.com|Alex       
s2.example.com|John       

There is missing

free.example.com|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
@kozlovsky
Copy link
Member

kozlovsky commented May 20, 2016

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

kozlovsky added a commit that referenced this issue Aug 22, 2016
# New features

* Added native JSON data type support in all supported databases: https://docs.ponyorm.com/json.html

# Backward incompatible changes

* Dropped Python 2.6 support

# Improvements

* #179 Added the compatibility with PYPY using psycopg2cffi
* Added an experimental @db_session `strict` parameter: https://docs.ponyorm.com/transactions.html#strict

# 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
Labels
Projects
None yet
Development

No branches or pull requests

2 participants