polymorphic mapper count() method does not use polymorphic_type

Issue #1067 resolved
Former user created an issue

When using .count() on a polymorphic mapper the polymorphic_type is not taken into account giving wrong results.

from sqlalchemy import *
from sqlalchemy.orm import *

metadata = MetaData()
animals = Table('animals', metadata,
      Column('id', Integer, primary_key=True),
      Column('animal_type', String(10), nullable=False))

class Animal(object):
    pass
class Dog(Animal):
    pass
class Cat(Animal):
    pass

mapper(Animal, animals,
       polymorphic_on=animals.c.animal_type)

mapper(Dog, inherits=Animal, polymorphic_identity='dog')
mapper(Cat, inherits=Animal, polymorphic_identity='cat')

def data():
    animals.insert().execute(
        {'id': 1, 'animal_type': 'dog'},
        {'id': 2, 'animal_type': 'dog'},
        {'id': 3, 'animal_type': 'cat'},
        {'id': 4, 'animal_type': 'cat'},
        {'id': 5, 'animal_type': 'cat'})


def test():
    s = create_session()

    py_count = len(s.query(Dog).all())

    sa_count = s.query(Dog).count()
    print 'py:', py_count, 'sa:', sa_count
    print 'correct?', sa_count == py_count


if __name__ == '__main__':
    metadata.bind = 'sqlite:///test'
    metadata.create_all()
    try:
        data()
        test()
    finally:
        metadata.drop_all()

Comments (2)

  1. Log in to comment