From: Mike Bayer Date: Sun, 30 Jan 2011 19:55:55 +0000 (-0500) Subject: - A warning is emitted when a joined-table inheriting mapper X-Git-Tag: rel_0_6_7~44 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b3c20d66f63871410c0ac170aae62fd35aa83126;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 f1a9c35a5e..15ec2f95de 100644 --- a/CHANGES +++ b/CHANGES @@ -10,6 +10,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 22b8f4f727..0245b7601b 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 +from sqlalchemy import sql, util, log, exc as sa_exc, schema from sqlalchemy.sql import expression, visitors, operators, util as sqlutil from sqlalchemy.orm import attributes, sync, exc as orm_exc, unitofwork from sqlalchemy.orm.interfaces import ( @@ -497,6 +497,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 52df52d5b2..164d4d4075 100644 --- a/test/orm/inheritance/test_basic.py +++ b/test/orm/inheritance/test_basic.py @@ -1346,6 +1346,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):