From: Mike Bayer Date: Sun, 30 Jan 2011 19:18:01 +0000 (-0500) Subject: - A warning is emitted when a joined-table inheriting mapper X-Git-Tag: rel_0_7b1~45 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7ad7dde6a5f0e14782834f64e8691266d8e75065;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - A warning is emitted when a joined-table inheriting mapper has no primary keys on the locally mapped table (but has pks on the superclass table). [ticket:2019] --- diff --git a/CHANGES b/CHANGES index bd8294b5f5..a8563a51d1 100644 --- a/CHANGES +++ b/CHANGES @@ -181,6 +181,10 @@ CHANGES a fixed name to be assigned to the alias object. [ticket:2030] + - A warning is emitted when a joined-table inheriting mapper + has no primary keys on the locally mapped table + (but has pks on the superclass table). [ticket:2019] + - sql - Column.copy(), as used in table.tometadata(), copies the 'doc' attribute. [ticket:2028] diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index 399c4436fd..c7e93c19d9 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -20,7 +20,7 @@ import operator from itertools import chain, groupby deque = __import__('collections').deque -from sqlalchemy import sql, util, log, exc as sa_exc, event +from sqlalchemy import sql, util, log, exc as sa_exc, event, schema from sqlalchemy.sql import expression, visitors, operators, util as sqlutil from sqlalchemy.orm import instrumentation, attributes, sync, \ exc as orm_exc, unitofwork, events @@ -505,6 +505,12 @@ class Mapper(object): "Mapper %s could not assemble any primary " "key columns for mapped table '%s'" % (self, self.mapped_table.description)) + elif self.local_table not in self._pks_by_table and \ + isinstance(self.local_table, schema.Table): + util.warn("Could not assemble any primary " + "keys for locally mapped table '%s' - " + "no rows will be persisted in this Table." + % self.local_table.description) if self.inherits and \ not self.concrete and \ diff --git a/test/orm/inheritance/test_basic.py b/test/orm/inheritance/test_basic.py index df7958b2ff..7257cbd791 100644 --- a/test/orm/inheritance/test_basic.py +++ b/test/orm/inheritance/test_basic.py @@ -1420,6 +1420,49 @@ class OptimizedLoadTest(_base.MappedTest): ), ) +class NoPKOnSubTableWarningTest(testing.TestBase): + + def _fixture(self): + metadata = MetaData() + parent = Table('parent', metadata, + Column('id', Integer, primary_key=True) + ) + child = Table('child', metadata, + Column('id', Integer, ForeignKey('parent.id')) + ) + return parent, child + + def tearDown(self): + clear_mappers() + + def test_warning_on_sub(self): + parent, child = self._fixture() + + class P(object): + pass + class C(P): + pass + + mapper(P, parent) + assert_raises_message( + sa_exc.SAWarning, + "Could not assemble any primary keys for locally mapped " + "table 'child' - no rows will be persisted in this Table.", + mapper, C, child, inherits=P + ) + + def test_no_warning_with_explicit(self): + parent, child = self._fixture() + + class P(object): + pass + class C(P): + pass + + mapper(P, parent) + mc = mapper(C, child, inherits=P, primary_key=[parent.c.id]) + eq_(mc.primary_key, (parent.c.id,)) + class PKDiscriminatorTest(_base.MappedTest): @classmethod def define_tables(cls, metadata):