diff options
| author | Jason Kirtland <jek@discorporate.us> | 2008-02-08 20:50:33 +0000 |
|---|---|---|
| committer | Jason Kirtland <jek@discorporate.us> | 2008-02-08 20:50:33 +0000 |
| commit | 1b228e848183b34046fc2ef34206344f88876f3a (patch) | |
| tree | 946671e17dd19e4ce4005ceb9811be90ecbceb56 /lib | |
| parent | 426b6d9baf35585698540fefc4ef2bed0e10242f (diff) | |
| download | sqlalchemy-1b228e848183b34046fc2ef34206344f88876f3a.tar.gz | |
- Added deferrability support to constraints
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/sqlalchemy/schema.py | 38 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/compiler.py | 14 |
2 files changed, 43 insertions, 9 deletions
diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index 64e9d203d..83f282b24 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -608,7 +608,7 @@ class ForeignKey(SchemaItem): constraint definition. """ - def __init__(self, column, constraint=None, use_alter=False, name=None, onupdate=None, ondelete=None): + def __init__(self, column, constraint=None, use_alter=False, name=None, onupdate=None, ondelete=None, deferrable=None, initially=None): """Construct a new ``ForeignKey`` object. column @@ -629,6 +629,8 @@ class ForeignKey(SchemaItem): self.name = name self.onupdate = onupdate self.ondelete = ondelete + self.deferrable = deferrable + self.initially = initially def __repr__(self): return "ForeignKey(%s)" % repr(self._get_colspec()) @@ -714,7 +716,7 @@ class ForeignKey(SchemaItem): self.parent.table.constraints.remove(fk.constraint) if self.constraint is None and isinstance(self.parent.table, Table): - self.constraint = ForeignKeyConstraint([],[], use_alter=self.use_alter, name=self.name, onupdate=self.onupdate, ondelete=self.ondelete) + self.constraint = ForeignKeyConstraint([],[], use_alter=self.use_alter, name=self.name, onupdate=self.onupdate, ondelete=self.ondelete, deferrable=self.deferrable, initially=self.initially) self.parent.table.append_constraint(self.constraint) self.constraint._append_fk(self) @@ -855,9 +857,11 @@ class Constraint(SchemaItem): list of underying columns. """ - def __init__(self, name=None): + def __init__(self, name=None, deferrable=None, initially=None): self.name = name self.columns = expression.ColumnCollection() + self.deferrable = deferrable + self.initially = initially def __contains__(self, x): return self.columns.contains_column(x) @@ -878,8 +882,8 @@ class Constraint(SchemaItem): raise NotImplementedError() class CheckConstraint(Constraint): - def __init__(self, sqltext, name=None): - super(CheckConstraint, self).__init__(name) + def __init__(self, sqltext, name=None, deferrable=None, initially=None): + super(CheckConstraint, self).__init__(name, deferrable, initially) self.sqltext = sqltext def __visit_name__(self): @@ -899,8 +903,8 @@ class CheckConstraint(Constraint): class ForeignKeyConstraint(Constraint): """Table-level foreign key constraint, represents a collection of ``ForeignKey`` objects.""" - def __init__(self, columns, refcolumns, name=None, onupdate=None, ondelete=None, use_alter=False): - super(ForeignKeyConstraint, self).__init__(name) + def __init__(self, columns, refcolumns, name=None, onupdate=None, ondelete=None, use_alter=False, deferrable=None, initially=None): + super(ForeignKeyConstraint, self).__init__(name, deferrable, initially) self.__colnames = columns self.__refcolnames = refcolumns self.elements = util.OrderedSet() @@ -930,7 +934,15 @@ class ForeignKeyConstraint(Constraint): class PrimaryKeyConstraint(Constraint): def __init__(self, *columns, **kwargs): - super(PrimaryKeyConstraint, self).__init__(name=kwargs.pop('name', None)) + constraint_args = dict(name=kwargs.pop('name', None), + deferrable=kwargs.pop('deferrable', None), + initially=kwargs.pop('initially', None)) + if kwargs: + raise exceptions.ArgumentError( + 'Unknown PrimaryKeyConstraint argument(s): %s' % + ', '.join([repr(x) for x in kwargs.keys()])) + + super(PrimaryKeyConstraint, self).__init__(**constraint_args) self.__colnames = list(columns) def _set_parent(self, table): @@ -959,7 +971,15 @@ class PrimaryKeyConstraint(Constraint): class UniqueConstraint(Constraint): def __init__(self, *columns, **kwargs): - super(UniqueConstraint, self).__init__(name=kwargs.pop('name', None)) + constraint_args = dict(name=kwargs.pop('name', None), + deferrable=kwargs.pop('deferrable', None), + initially=kwargs.pop('initially', None)) + if kwargs: + raise exceptions.ArgumentError( + 'Unknown UniqueConstraint argument(s): %s' % + ', '.join([repr(x) for x in kwargs.keys()])) + + super(UniqueConstraint, self).__init__(**constraint_args) self.__colnames = list(columns) def _set_parent(self, table): diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 43950a9a6..02f6efce1 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -844,9 +844,11 @@ class SchemaGenerator(DDLBase): self.append("CONSTRAINT %s " % self.preparer.format_constraint(constraint)) self.append(" CHECK (%s)" % constraint.sqltext) + self.define_constraint_deferrability(constraint) def visit_column_check_constraint(self, constraint): self.append(" CHECK (%s)" % constraint.sqltext) + self.define_constraint_deferrability(constraint) def visit_primary_key_constraint(self, constraint): if len(constraint) == 0: @@ -856,6 +858,7 @@ class SchemaGenerator(DDLBase): self.append("CONSTRAINT %s " % self.preparer.format_constraint(constraint)) self.append("PRIMARY KEY ") self.append("(%s)" % ', '.join([self.preparer.quote(c, c.name) for c in constraint])) + self.define_constraint_deferrability(constraint) def visit_foreign_key_constraint(self, constraint): if constraint.use_alter and self.dialect.supports_alter: @@ -883,6 +886,7 @@ class SchemaGenerator(DDLBase): self.append(" ON DELETE %s" % constraint.ondelete) if constraint.onupdate is not None: self.append(" ON UPDATE %s" % constraint.onupdate) + self.define_constraint_deferrability(constraint) def visit_unique_constraint(self, constraint): self.append(", \n\t") @@ -890,6 +894,16 @@ class SchemaGenerator(DDLBase): self.append("CONSTRAINT %s " % self.preparer.format_constraint(constraint)) self.append(" UNIQUE (%s)" % (', '.join([self.preparer.quote(c, c.name) for c in constraint]))) + self.define_constraint_deferrability(constraint) + + def define_constraint_deferrability(self, constraint): + if constraint.deferrable is not None: + if constraint.deferrable: + self.append(" DEFERRABLE") + else: + self.append(" NOT DEFERRABLE") + if constraint.initially is not None: + self.append(" INITIALLY %s" % constraint.initially) def visit_column(self, column): pass |
