]> 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:55:55 +0000 (14:55 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 30 Jan 2011 19:55:55 +0000 (14:55 -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 f1a9c35a5ec3fbcfb7dcdec45493ef962f21c8b9..15ec2f95de990c4907994f7850a9ab87f2415384 100644 (file)
--- 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]
index 22b8f4f7276a0b0fd47681a5e188032e79d48917..0245b7601b03e270ae044cc17d3564c1c4e2b6d6 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
+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 \
index 52df52d5b2688044af92dcffb832b6c06e9017da..164d4d4075a3cf61eefada12254306b2ffde944f 100644 (file)
@@ -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):