summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/orm/mapper.py11
-rw-r--r--lib/sqlalchemy/sql_util.py19
2 files changed, 22 insertions, 8 deletions
diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py
index bde887b2a..3e9f2724c 100644
--- a/lib/sqlalchemy/orm/mapper.py
+++ b/lib/sqlalchemy/orm/mapper.py
@@ -193,11 +193,12 @@ class Mapper(object):
l = self.pks_by_table[t]
except KeyError:
l = self.pks_by_table.setdefault(t, util.HashSet(ordered=True))
- if not len(t.primary_key):
- raise exceptions.ArgumentError("Table " + t.name + " has no primary key columns. Specify primary_key argument to mapper.")
for k in t.primary_key:
l.append(k)
-
+
+ if len(self.pks_by_table[self.mapped_table]) == 0:
+ raise exceptions.ArgumentError("Could not assemble any primary key columsn from given tables for table '%s'" % (self.mapped_table.name))
+
# make table columns addressable via the mapper
self.columns = util.OrderedProperties()
self.c = self.columns
@@ -532,7 +533,7 @@ class Mapper(object):
list."""
#print "SAVE_OBJ MAPPER", self.class_.__name__, objects
connection = uow.transaction.connection(self)
- for table in self.tables:
+ for table in self.tables.sort(reverse=False):
#print "SAVE_OBJ table ", self.class_.__name__, table.name
# looping through our set of tables, which are all "real" tables, as opposed
# to our main table which might be a select statement or something non-writeable
@@ -700,7 +701,7 @@ class Mapper(object):
connection = uow.transaction.connection(self)
#print "DELETE_OBJ MAPPER", self.class_.__name__, objects
- for table in util.reversed(self.tables):
+ for table in self.tables.sort(reverse=True):
if not self._has_pks(table):
continue
delete = []
diff --git a/lib/sqlalchemy/sql_util.py b/lib/sqlalchemy/sql_util.py
index 0728bba47..1a720d1d5 100644
--- a/lib/sqlalchemy/sql_util.py
+++ b/lib/sqlalchemy/sql_util.py
@@ -9,7 +9,22 @@ class TableCollection(object):
self.tables = []
def add(self, table):
self.tables.append(table)
- def sort(self, reverse=False ):
+ if hasattr(self, '_sorted'):
+ del self._sorted
+ def sort(self, reverse=False):
+ try:
+ sorted = self._sorted
+ except AttributeError, e:
+ self._sorted = self._do_sort()
+ return self.sort(reverse=reverse)
+ if reverse:
+ x = sorted[:]
+ x.reverse()
+ return x
+ else:
+ return sorted
+
+ def _do_sort(self):
import sqlalchemy.orm.topological
tuples = []
class TVisitor(schema.SchemaVisitor):
@@ -29,8 +44,6 @@ class TableCollection(object):
to_sequence( child )
if head is not None:
to_sequence( head )
- if reverse:
- sequence.reverse()
return sequence