]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
added allow_null_pks option to Mapper, allows rows where some
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 21 Jul 2006 16:11:11 +0000 (16:11 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 21 Jul 2006 16:11:11 +0000 (16:11 +0000)
primary key columns are null (i.e. when mapping to outer joins etc)

CHANGES
lib/sqlalchemy/orm/mapper.py

diff --git a/CHANGES b/CHANGES
index 94b2e0e8e086a3bb635db5fd9f529550b17d5a48..8e03c8eec4525ae1b3069c14bb46752f8ab948dc 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,8 @@
 0.2.7
 - assignmapper was setting is_primary=True, causing all sorts of mayhem
 by not raising an error when redundant mappers were set up, fixed
+- added allow_null_pks option to Mapper, allows rows where some
+primary key columns are null (i.e. when mapping to outer joins etc)
 
 0.2.6
 - big overhaul to schema to allow truly composite primary and foreign
index 7cc99b2259d923a3c96ff271176c64f032e73602..c9c342dc92b768dae3cced6d063443111605e900 100644 (file)
@@ -52,7 +52,8 @@ class Mapper(object):
                 polymorphic_map=None,
                 polymorphic_identity=None,
                 concrete=False,
-                select_table=None):
+                select_table=None,
+                allow_null_pks=False):
 
         if not issubclass(class_, object):
             raise exceptions.ArgumentError("Class '%s' is not a new-style class" % class_.__name__)
@@ -84,6 +85,7 @@ class Mapper(object):
         self.extension = extension
         self.properties = properties or {}
         self.allow_column_override = allow_column_override
+        self.allow_null_pks = allow_null_pks
         
         # a Column which is used during a select operation to retrieve the 
         # "polymorphic identity" of the row, which indicates which Mapper should be used
@@ -981,11 +983,21 @@ class Mapper(object):
         # look in result-local identitymap for it.
         exists = imap.has_key(identitykey)      
         if not exists:
-            # check if primary key cols in the result are None - this indicates 
-            # an instance of the object is not present in the row
-            for col in self.pks_by_table[self.mapped_table]:
-                if row[col] is None:
+            if self.allow_null_pks:
+                # check if *all* primary key cols in the result are None - this indicates 
+                # an instance of the object is not present in the row.  
+                for col in self.pks_by_table[self.mapped_table]:
+                    if row[col] is not None:
+                        break
+                else:
                     return None
+            else:
+                # otherwise, check if *any* primary key cols in the result are None - this indicates 
+                # an instance of the object is not present in the row.  
+                for col in self.pks_by_table[self.mapped_table]:
+                    if row[col] is None:
+                        return None
+            
             # plugin point
             instance = self.extension.create_instance(self, session, row, imap, self.class_)
             if instance is EXT_PASS: