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

Polymorphic queries #30

Open
bitozoid opened this issue Aug 27, 2021 · 1 comment
Open

Polymorphic queries #30

bitozoid opened this issue Aug 27, 2021 · 1 comment

Comments

@bitozoid
Copy link

I have replicated the example in https://ming.readthedocs.io/en/latest/polymorphism.html.

I have added a Bike and can reproduce the queries below as in the documentation.

from ming import schema
from ming.odm import FieldProperty
from ming.odm.declarative import MappedClass

class Transport(MappedClass):
    class __mongometa__:
        session = session
        name = 'transport'
        polymorphic_on = '_type'
        polymorphic_identity = 'base'

    _id = FieldProperty(schema.ObjectId)
    origin = FieldProperty(schema.String(required=True))
    destination = FieldProperty(schema.String(if_missing=''))
    _type = FieldProperty(schema.String(if_missing='base'))

    def move(self):
        return 'moving from {} to {}'.format(self.origin, self.destination)

class Bus(Transport):
    class __mongometa__:
        polymorphic_identity = 'bus'

    _type=FieldProperty(str, if_missing='bus')
    passengers_count = FieldProperty(schema.Int(if_missing=0))

    def move(self):
        return 'driving from {} to {}'.format(self.origin, self.destination)


class AirBus(Bus):
    class __mongometa__:
        polymorphic_identity = 'airbus'

    _type=FieldProperty(str, if_missing='airbus')
    wings_count = FieldProperty(schema.Int(if_missing=2))

    def move(self):
        return 'flying from {} to {}'.format(self.origin, self.destination)

class Bike(Transport):
    class __mongometa__:
        polymorphic_identity = 'bike'

    _type=FieldProperty(str, if_missing='bike')

    def move(self):
        return 'cycling from {} to {}'.format(self.origin, self.destination)


Bus(origin='Rome', destination='London', passengers_count=20))
Bus(origin='Turin', destination='London', passengers_count=20))
AirBus(origin='Turin', destination='London', passengers_count=60, wings_count=3))
Bike(origin='Newcastle', destination='London'))
session.flush()
session.clear()

print(Transport.query.find().count())  # 4
print(Transport.query.find({'_type': 'bus'}).count())  # 2
print(Transport.query.find({'_type': 'airbus'}).count()) # 1

However I have also added next queries and would expect this to produce 3 and 1:

print(Bus.query.find().count())  # expected 3, got 4
print(AirBus.query.find().count())  # expected 1, got 4

but I get 4 and 4.

@brondsem
Copy link
Collaborator

brondsem commented Sep 1, 2021

I agree that would be nice. I don't know if ming automatically adjusts find() criteria anywhere else, but it could be worth it for this situation. Probably also update() and find_and_modify() and several others

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

2 participants