]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- A warning is emitted when a joined-table inheriting mapper
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 30 Jan 2011 19:18:01 +0000 (14:18 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 30 Jan 2011 19:18:01 +0000 (14:18 -0500)
    has no primary keys on the locally mapped table
    (but has pks on the superclass table).  [ticket:2019]

CHANGES
lib/sqlalchemy/orm/mapper.py
test/orm/inheritance/test_basic.py

diff --git a/CHANGES b/CHANGES
index bd8294b5f549d6a58e563c1b7b0fc94c0b0eeb20..a8563a51d113fef9f41626999d2f6399f3b8b0bc 100644 (file)
--- 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]
index 399c4436fd6a11a6e61749b0d635825daae6dc96..c7e93c19d9fc0c2bc5598b23e0c340e710b39622 100644 (file)
@@ -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 \
index df7958b2ff2ae0d855f0ad6a4f903884c2f30e26..7257cbd7915e6ca8df28ba4407aa2d57b367e2ab 100644 (file)
@@ -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):