summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2011-02-25 13:20:43 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2011-02-25 13:20:43 -0500
commita7f766d7c7fd6c53eb0019e32569e915b3f31eb4 (patch)
tree25f6234f67bbea40bb95eb34572bd2f67136e33f /lib/sqlalchemy
parent34d6c9bc7dafbb2717ae10b6e11942e2dd409f35 (diff)
downloadsqlalchemy-a7f766d7c7fd6c53eb0019e32569e915b3f31eb4.tar.gz
- establish an "insert" option for events to control ordering if needed (not needed yet tho)
- render foreign key constraints in the order in which they were cerated
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/event.py23
-rw-r--r--lib/sqlalchemy/schema.py7
-rw-r--r--lib/sqlalchemy/sql/compiler.py2
3 files changed, 29 insertions, 3 deletions
diff --git a/lib/sqlalchemy/event.py b/lib/sqlalchemy/event.py
index 7c2b49ce8..b2e5cd00f 100644
--- a/lib/sqlalchemy/event.py
+++ b/lib/sqlalchemy/event.py
@@ -143,8 +143,11 @@ class Events(object):
return None
@classmethod
- def _listen(cls, target, identifier, fn, propagate=False):
- getattr(target.dispatch, identifier).append(fn, target, propagate)
+ def _listen(cls, target, identifier, fn, propagate=False, insert=False):
+ if insert:
+ getattr(target.dispatch, identifier).insert(fn, target, propagate)
+ else:
+ getattr(target.dispatch, identifier).append(fn, target, propagate)
@classmethod
def _remove(cls, target, identifier, fn):
@@ -164,6 +167,16 @@ class _DispatchDescriptor(object):
self.__doc__ = fn.__doc__
self._clslevel = util.defaultdict(list)
+ def insert(self, obj, target, propagate):
+ assert isinstance(target, type), \
+ "Class-level Event targets must be classes."
+
+ stack = [target]
+ while stack:
+ cls = stack.pop(0)
+ stack.extend(cls.__subclasses__())
+ self._clslevel[cls].insert(0, obj)
+
def append(self, obj, target, propagate):
assert isinstance(target, type), \
"Class-level Event targets must be classes."
@@ -260,6 +273,12 @@ class _ListenerCollection(object):
and not only_propagate or l in self.propagate
])
+ def insert(self, obj, target, propagate):
+ if obj not in self.listeners:
+ self.listeners.insert(0, obj)
+ if propagate:
+ self.propagate.add(obj)
+
def append(self, obj, target, propagate):
if obj not in self.listeners:
self.listeners.append(obj)
diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py
index e6b970291..80d6018b8 100644
--- a/lib/sqlalchemy/schema.py
+++ b/lib/sqlalchemy/schema.py
@@ -265,6 +265,12 @@ class Table(SchemaItem, expression.TableClause):
# allow user-overrides
self._init_items(*args)
+ @property
+ def _sorted_constraints(self):
+ """Return the set of constraints as a list, sorted by creation order."""
+
+ return sorted(self.constraints, key=lambda c:c._creation_order)
+
def _init_existing(self, *args, **kwargs):
autoload = kwargs.pop('autoload', False)
autoload_with = kwargs.pop('autoload_with', None)
@@ -1595,6 +1601,7 @@ class Constraint(SchemaItem):
self.deferrable = deferrable
self.initially = initially
self._create_rule = _create_rule
+ util.set_creation_order(self)
@property
def table(self):
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index 781072dd0..d6a020bdc 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -1272,7 +1272,7 @@ class DDLCompiler(engine.Compiled):
if table.primary_key:
constraints.append(table.primary_key)
- constraints.extend([c for c in table.constraints
+ constraints.extend([c for c in table._sorted_constraints
if c is not table.primary_key])
return ", \n\t".join(p for p in