From: Mike Bayer Date: Fri, 8 Aug 2008 04:25:17 +0000 (+0000) Subject: - Fixed bug regarding inherit_condition passed X-Git-Tag: rel_0_4_8~13 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=924ffe6dfa01e6fa5e264604a24119363cbc07b5;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Fixed bug regarding inherit_condition passed with "A=B" versus "B=A" leading to errors [ticket:1039] --- diff --git a/CHANGES b/CHANGES index e31a4ba38c..acba941d80 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,13 @@ ======= CHANGES ======= +0.4.8 +===== +- orm + - Fixed bug regarding inherit_condition passed + with "A=B" versus "B=A" leading to errors + [ticket:1039] + 0.4.7p1 ===== - orm diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index d39e743e01..b86fdc07b1 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -1578,12 +1578,13 @@ class Mapper(object): def visit_binary(binary): leftcol = binary.left rightcol = binary.right + if leftcol is None or rightcol is None: return if leftcol.table not in needs_tables: binary.left = sql.bindparam(None, None, type_=binary.right.type) param_names.append((leftcol, binary.left)) - elif rightcol not in needs_tables: + elif rightcol.table not in needs_tables: binary.right = sql.bindparam(None, None, type_=binary.right.type) param_names.append((rightcol, binary.right)) @@ -1595,7 +1596,7 @@ class Mapper(object): break if not mapper.single: allconds.append(visitors.traverse(mapper.inherit_condition, clone=True, visit_binary=visit_binary)) - + return sql.and_(*allconds), param_names Mapper.logger = logging.class_logger(Mapper) diff --git a/test/orm/inheritance/basic.py b/test/orm/inheritance/basic.py index cb754fed59..f59973d026 100644 --- a/test/orm/inheritance/basic.py +++ b/test/orm/inheritance/basic.py @@ -275,7 +275,42 @@ class GetTest(ORMTest): test_get_polymorphic = create_test(True, 'test_get_polymorphic') test_get_nonpolymorphic = create_test(False, 'test_get_nonpolymorphic') +class InheritConditionTest(ORMTest): + def define_tables(self, metadata): + global base, child + base = Table('base', metadata, + Column('id', Integer, primary_key = True), + Column('type', String(40))) + + child = Table('child', metadata, + Column('base_id', Integer, ForeignKey(base.c.id), primary_key = True), + Column('title', String(64)), + Column('parent_id', Integer, ForeignKey('child.base_id')) + ) + + def test_inherit_cond(self): + class Base(fixtures.Base): + pass + class Child(Base): + pass + + mapper(Base, base, polymorphic_on=base.c.type) + mapper(Child, child, inherits=Base, inherit_condition=child.c.base_id==base.c.id, polymorphic_identity='c') + + sess = create_session() + sess.save(Child(title='c1')) + sess.save(Child(title='c2')) + sess.flush() + sess.clear() + for inherit_cond in (child.c.base_id==base.c.id, base.c.id==child.c.base_id): + clear_mappers() + mapper(Base, base, polymorphic_on=base.c.type) + mapper(Child, child, inherits=Base, inherit_condition=inherit_cond, polymorphic_identity='c') + + assert sess.query(Base).order_by(Base.id).all() == [Child(title='c1'), Child(title='c2')] + + class ConstructionTest(ORMTest): def define_tables(self, metadata): global content_type, content, product