<p dir="auto"><strong>Migrated issue, originally created by Anonymous</strong></p>
<p dir="auto">When using .count() on a polymorphic mapper the polymorphic_type is not taken into account giving wrong results.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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()"><pre class="notranslate"><code class="notranslate">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()
</code></pre></div>
<p dir="auto"><strong>Migrated issue, originally created by Denis Otkidach (<a href="https://github.com/ods">@ods</a>)</strong></p>
<p dir="auto">New object with the same ID as existing one implicitly merged via cascade rules has old value of relationship property after the commit when the new value is None.</p>
<p dir="auto">In short:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="doc.doc_links = [DocLink(id=doc_link_id, link=None)]
session.commit()
assert doc.doc_links[0].link is None  # fails"><pre class="notranslate"><code class="notranslate">doc.doc_links = [DocLink(id=doc_link_id, link=None)]
session.commit()
assert doc.doc_links[0].link is None  # fails
</code></pre></div>
<p dir="auto">Full test is attached (test3 is the test case that fails).</p>
<hr>
<p dir="auto">Attachments: <a href="../wiki/imported_issue_attachments/3060/sa_merge_relation.py">sa_merge_relation.py</a> | <a href="../wiki/imported_issue_attachments/3060/3060.patch">3060.patch</a></p>