TurboGears / TurboGears/Ming

Polymorphic queries

Open
#30 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
30
Forks
17
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the polymorphism example at ming.readthedocs.io/en/latest/polymorphism.html and trace how Bus.query.find() and AirBus.query.find() build their queries. Reproduce the four inserted records and the reported counts, then identify the query behavior that makes both subclass queries return all four records. Done means the subclass counts match the documented polymorphic identities and relevant tests cover the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.