From 7f87262894d26e111d72b1f948bd4faef84ff4d4 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 21 Jul 2006 16:11:11 +0000 Subject: [PATCH] added allow_null_pks option to Mapper, allows rows where some primary key columns are null (i.e. when mapping to outer joins etc) --- CHANGES | 2 ++ lib/sqlalchemy/orm/mapper.py | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index 94b2e0e8e0..8e03c8eec4 100644 --- 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 diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index 7cc99b2259..c9c342dc92 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -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: -- 2.47.2