summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/schema.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqlalchemy/schema.py')
-rw-r--r--lib/sqlalchemy/schema.py51
1 files changed, 31 insertions, 20 deletions
diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py
index 2e5a1a637..70087ee73 100644
--- a/lib/sqlalchemy/schema.py
+++ b/lib/sqlalchemy/schema.py
@@ -1261,7 +1261,8 @@ class Constraint(SchemaItem):
__visit_name__ = 'constraint'
- def __init__(self, name=None, deferrable=None, initially=None, inline_ddl=True):
+ def __init__(self, name=None, deferrable=None, initially=None,
+ _create_rule=None):
"""Create a SQL constraint.
name
@@ -1275,20 +1276,29 @@ class Constraint(SchemaItem):
Optional string. If set, emit INITIALLY <value> when issuing DDL
for this constraint.
- inline_ddl
- if True, DDL for this Constraint will be generated within the span of a
- CREATE TABLE or DROP TABLE statement, when the associated table's
- DDL is generated. if False, no DDL is issued within that process.
- Instead, it is expected that an AddConstraint or DropConstraint
- construct will be used to issue DDL for this Contraint.
- The AddConstraint/DropConstraint constructs set this flag automatically
- as well.
+ _create_rule
+ a callable which is passed the DDLCompiler object during
+ compilation. Returns True or False to signal inline generation of
+ this Constraint.
+
+ The AddConstraint and DropConstraint DDL constructs provide
+ DDLElement's more comprehensive "conditional DDL" approach that is
+ passed a database connection when DDL is being issued. _create_rule
+ is instead called during any CREATE TABLE compilation, where there
+ may not be any transaction/connection in progress. However, it
+ allows conditional compilation of the constraint even for backends
+ which do not support addition of constraints through ALTER TABLE,
+ which currently includes SQLite.
+
+ _create_rule is used by some types to create constraints.
+ Currently, its call signature is subject to change at any time.
+
"""
self.name = name
self.deferrable = deferrable
self.initially = initially
- self.inline_ddl = inline_ddl
+ self._create_rule = _create_rule
@property
def table(self):
@@ -1364,13 +1374,13 @@ class CheckConstraint(Constraint):
Can be included in the definition of a Table or Column.
"""
- def __init__(self, sqltext, name=None, deferrable=None, initially=None, table=None):
+ def __init__(self, sqltext, name=None, deferrable=None, initially=None, table=None, _create_rule=None):
"""Construct a CHECK constraint.
sqltext
- A string containing the constraint definition. Will be used
- verbatim.
-
+ A string containing the constraint definition, which will be used
+ verbatim, or a SQL expression construct.
+
name
Optional, the in-database name of the constraint.
@@ -1384,11 +1394,8 @@ class CheckConstraint(Constraint):
"""
- super(CheckConstraint, self).__init__(name, deferrable, initially)
- if not isinstance(sqltext, basestring):
- raise exc.ArgumentError(
- "sqltext must be a string and will be used verbatim.")
- self.sqltext = sqltext
+ super(CheckConstraint, self).__init__(name, deferrable, initially, _create_rule)
+ self.sqltext = expression._literal_as_text(sqltext)
if table is not None:
self._set_parent(table)
@@ -2224,7 +2231,6 @@ class _CreateDropBase(DDLElement):
self._check_ddl_on(on)
self.on = on
self.bind = bind
- element.inline_ddl = False
class CreateTable(_CreateDropBase):
"""Represent a CREATE TABLE statement."""
@@ -2265,6 +2271,10 @@ class AddConstraint(_CreateDropBase):
__visit_name__ = "add_constraint"
+ def __init__(self, element, *args, **kw):
+ super(AddConstraint, self).__init__(element, *args, **kw)
+ element._create_rule = lambda compiler: False
+
class DropConstraint(_CreateDropBase):
"""Represent an ALTER TABLE DROP CONSTRAINT statement."""
@@ -2273,6 +2283,7 @@ class DropConstraint(_CreateDropBase):
def __init__(self, element, cascade=False, **kw):
self.cascade = cascade
super(DropConstraint, self).__init__(element, **kw)
+ element._create_rule = lambda compiler: False
def _bind_or_error(schemaitem):
bind = schemaitem.bind