diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2005-09-15 05:18:15 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2005-09-15 05:18:15 +0000 |
commit | 590f19e19090d8ce1edb19f15fd20ca2d5c56e3b (patch) | |
tree | 4cb1586557fe26f6187148a5686b3c2a3b62db7e /lib/sqlalchemy/mapper.py | |
parent | a6360fdcf5e97db64059dc06b48052301f6a90cc (diff) | |
download | sqlalchemy-590f19e19090d8ce1edb19f15fd20ca2d5c56e3b.tar.gz |
Diffstat (limited to 'lib/sqlalchemy/mapper.py')
-rw-r--r-- | lib/sqlalchemy/mapper.py | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/lib/sqlalchemy/mapper.py b/lib/sqlalchemy/mapper.py index 808a63914..4d9ea3710 100644 --- a/lib/sqlalchemy/mapper.py +++ b/lib/sqlalchemy/mapper.py @@ -186,7 +186,8 @@ class Mapper(object): return None def put(self, instance): - key = objectstore.get_instance_key(instance, self.class_, self.table, self.primary_keys[self.selectable]) + key = objectstore.get_instance_key(instance, self.class_, self.table, self.primary_keys[self.selectable], self) + print repr(instance.__dict__) objectstore.put(key, instance, self.scope) return key @@ -232,34 +233,36 @@ class Mapper(object): def save_obj(self, objects): # try to get inserts to be en-masse with the "guess-the-id" thing maybe - work = {} - for table in self.tables: - work[table] = {'insert': [], 'update': []} - for obj in objects: - for table in self.tables: + for table in self.tables: + # loop thru tables in the outer loop, objects on the inner loop. + # this is important for an object represented across two tables + # so that it gets its primary keys populated for the benefit of the + # second table. + insert = [] + update = [] + for obj in objects: params = {} for col in table.columns: params[col.key] = self._getattrbycolumn(obj, col) if hasattr(obj, "_instance_key"): - work[table]['update'].append(params) + update.append(params) else: - work[table]['insert'].append((obj, params)) + insert.append((obj, params)) - for table, stuff in work.iteritems(): - if len(stuff['update']): + if len(update): clause = sql.and_() for col in self.primary_keys[table]: clause.clauses.append(col == sql.bindparam(col.key)) statement = table.update(clause) statement.echo = self.echo - statement.execute(*stuff['update']) + statement.execute(*update) - if len(stuff['insert']): + if len(insert): statement = table.insert() statement.echo = self.echo - for rec in stuff['insert']: + for rec in insert: (obj, params) = rec statement.execute(**params) primary_key = table.engine.last_inserted_ids()[0] |