[Sqlalchemy-commits] sqlalchemy: - a warning is emitted in mapper() if the polymorphi...
Brought to you by:
zzzeek
From: <co...@sq...> - 2010-08-11 17:17:25
|
details: http://hg.sqlalchemy.org/sqlalchemy/sqlalchemy/rev/f9c64cf0cbb9 changeset: 6743:f9c64cf0cbb9 user: zzzeek date: Wed Aug 11 11:57:56 2010 -0400 description: - a warning is emitted in mapper() if the polymorphic_on column is not present either in direct or derived form in the mapped selectable or in the with_polymorphic selectable, instead of silently ignoring it. Look for this to become an exception in 0.7. diffstat: CHANGES | 7 +++ lib/sqlalchemy/orm/mapper.py | 7 +++ test/orm/inheritance/test_basic.py | 68 +++++++++++++++++++++++++++++++++++-- 3 files changed, 78 insertions(+), 4 deletions(-) diffs (124 lines): diff -r a4cfae094eac -r f9c64cf0cbb9 CHANGES --- a/CHANGES Tue Aug 10 10:24:02 2010 -0400 +++ b/CHANGES Wed Aug 11 11:57:56 2010 -0400 @@ -15,6 +15,13 @@ ConcurrentModificationError in an "except:" clause. + - a warning is emitted in mapper() if the polymorphic_on + column is not present either in direct or derived + form in the mapped selectable or in the + with_polymorphic selectable, instead of silently + ignoring it. Look for this to become an + exception in 0.7. + - Moving an o2m object from one collection to another, or vice versa changing the referenced object by an m2o, where the foreign key is also a diff -r a4cfae094eac -r f9c64cf0cbb9 lib/sqlalchemy/orm/mapper.py --- a/lib/sqlalchemy/orm/mapper.py Tue Aug 10 10:24:02 2010 -0400 +++ b/lib/sqlalchemy/orm/mapper.py Wed Aug 11 11:57:56 2010 -0400 @@ -574,6 +574,13 @@ if col is None: instrument = False col = self.polymorphic_on + if self.with_polymorphic is None \ + or self.with_polymorphic[1].corresponding_column(col) \ + is None: + util.warn("Could not map polymoprhic_on column " + "'%s' to the mapped table - polymorphic " + "loads will not function properly" + % col.description) else: instrument = True if self._should_exclude(col.key, col.key, local=False): diff -r a4cfae094eac -r f9c64cf0cbb9 test/orm/inheritance/test_basic.py --- a/test/orm/inheritance/test_basic.py Tue Aug 10 10:24:02 2010 -0400 +++ b/test/orm/inheritance/test_basic.py Wed Aug 11 11:57:56 2010 -0400 @@ -16,7 +16,8 @@ def define_tables(cls, metadata): global foo, bar, blub foo = Table('foo', metadata, - Column('id', Integer, primary_key=True, test_needs_autoincrement=True), + Column('id', Integer, primary_key=True, + test_needs_autoincrement=True), Column('data', String(20))) bar = Table('bar', metadata, @@ -60,14 +61,73 @@ b1.parent_foo = f b2.parent_foo = f sess.flush() - compare = ','.join([repr(b1), repr(b2), repr(b1.parent_foo), repr(b2.parent_foo)]) + compare = ','.join([repr(b1), repr(b2), repr(b1.parent_foo), + repr(b2.parent_foo)]) sess.expunge_all() l = sess.query(Blub).all() - result = ','.join([repr(l[0]), repr(l[1]), repr(l[0].parent_foo), repr(l[1].parent_foo)]) + result = ','.join([repr(l[0]), repr(l[1]), + repr(l[0].parent_foo), repr(l[1].parent_foo)]) print compare print result self.assert_(compare == result) - self.assert_(l[0].parent_foo.data == 'foo #1' and l[1].parent_foo.data == 'foo #1') + self.assert_(l[0].parent_foo.data == 'foo #1' + and l[1].parent_foo.data == 'foo #1') + +class PolymorphicOnNotLocalTest(_base.MappedTest): + @classmethod + def define_tables(cls, metadata): + t1 = Table('t1', metadata, + Column('id', Integer, primary_key=True), + Column('x', String(10)), + Column('q', String(10))) + t2 = Table('t2', metadata, + Column('id', Integer, primary_key=True), + Column('y', String(10)), + Column('xid', ForeignKey('t1.x'))) + + @testing.resolve_artifact_names + def test_bad_polymorphic_on(self): + class InterfaceBase(object): + pass + + t1t2_join = select([t1.c.x], from_obj=[t1.join(t2)]).alias() + def go(): + interface_m = mapper(InterfaceBase, t2, + polymorphic_on=t1t2_join.c.x, + polymorphic_identity=0) + + assert_raises_message( + sa_exc.SAWarning, + "Could not map polymoprhic_on column 'x' to the mapped table - " + "polymorphic loads will not function properly", + go + ) + clear_mappers() + + # if its in the with_polymorphic, then its OK + interface_m = mapper(InterfaceBase, t2, + polymorphic_on=t1t2_join.c.x, + with_polymorphic=('*', t1t2_join), + polymorphic_identity=0) + compile_mappers() + + clear_mappers() + + # if with_polymorphic, but its not present, not OK + def go(): + t1t2_join_2 = select([t1.c.q], from_obj=[t1.join(t2)]).alias() + interface_m = mapper(InterfaceBase, t2, + polymorphic_on=t1t2_join.c.x, + with_polymorphic=('*', t1t2_join_2), + polymorphic_identity=0) + assert_raises_message( + sa_exc.SAWarning, + "Could not map polymoprhic_on column 'x' to the mapped table - " + "polymorphic loads will not function properly", + go + ) + + class FalseDiscriminatorTest(_base.MappedTest): @classmethod |