SQLAlchemy 1.1 Documentation
Customizing DDL¶
In the preceding sections we’ve discussed a variety of schema constructs
including Table,
ForeignKeyConstraint,
CheckConstraint, and
Sequence. Throughout, we’ve relied upon the
create() and create_all() methods of
Table and MetaData in
order to issue data definition language (DDL) for all constructs. When issued,
a pre-determined order of operations is invoked, and DDL to create each table
is created unconditionally including all constraints and other objects
associated with it. For more complex scenarios where database-specific DDL is
required, SQLAlchemy offers two techniques which can be used to add any DDL
based on any condition, either accompanying the standard generation of tables
or by itself.
Custom DDL¶
Custom DDL phrases are most easily achieved using the
DDL construct. This construct works like all the
other DDL elements except it accepts a string which is the text to be emitted:
event.listen(
metadata,
"after_create",
DDL("ALTER TABLE users ADD CONSTRAINT "
"cst_user_name_length "
" CHECK (length(user_name) >= 8)")
)A more comprehensive method of creating libraries of DDL constructs is to use custom compilation - see Custom SQL Constructs and Compilation Extension for details.
Controlling DDL Sequences¶
The DDL construct introduced previously also has the
ability to be invoked conditionally based on inspection of the
database. This feature is available using the DDLElement.execute_if()
method. For example, if we wanted to create a trigger but only on
the Postgresql backend, we could invoke this as:
mytable = Table(
'mytable', metadata,
Column('id', Integer, primary_key=True),
Column('data', String(50))
)
trigger = DDL(
"CREATE TRIGGER dt_ins BEFORE INSERT ON mytable "
"FOR EACH ROW BEGIN SET NEW.data='ins'; END"
)
event.listen(
mytable,
'after_create',
trigger.execute_if(dialect='postgresql')
)The DDLElement.execute_if.dialect keyword also accepts a tuple
of string dialect names:
event.listen(
mytable,
"after_create",
trigger.execute_if(dialect=('postgresql', 'mysql'))
)
event.listen(
mytable,
"before_drop",
trigger.execute_if(dialect=('postgresql', 'mysql'))
)The DDLElement.execute_if() method can also work against a callable
function that will receive the database connection in use. In the
example below, we use this to conditionally create a CHECK constraint,
first looking within the Postgresql catalogs to see if it exists:
def should_create(ddl, target, connection, **kw):
row = connection.execute(
"select conname from pg_constraint where conname='%s'" %
ddl.element.name).scalar()
return not bool(row)
def should_drop(ddl, target, connection, **kw):
return not should_create(ddl, target, connection, **kw)
event.listen(
users,
"after_create",
DDL(
"ALTER TABLE users ADD CONSTRAINT "
"cst_user_name_length CHECK (length(user_name) >= 8)"
).execute_if(callable_=should_create)
)
event.listen(
users,
"before_drop",
DDL(
"ALTER TABLE users DROP CONSTRAINT cst_user_name_length"
).execute_if(callable_=should_drop)
)
sqlusers.create(engine)
CREATE TABLE users (
user_id SERIAL NOT NULL,
user_name VARCHAR(40) NOT NULL,
PRIMARY KEY (user_id)
)
select conname from pg_constraint where conname='cst_user_name_length'
ALTER TABLE users ADD CONSTRAINT cst_user_name_length CHECK (length(user_name) >= 8)
sqlusers.drop(engine)
select conname from pg_constraint where conname='cst_user_name_length'
ALTER TABLE users DROP CONSTRAINT cst_user_name_length
DROP TABLE usersUsing the built-in DDLElement Classes¶
The sqlalchemy.schema package contains SQL expression constructs that
provide DDL expressions. For example, to produce a CREATE TABLE statement:
from sqlalchemy.schema import CreateTable
sqlengine.execute(CreateTable(mytable))
CREATE TABLE mytable (
col1 INTEGER,
col2 INTEGER,
col3 INTEGER,
col4 INTEGER,
col5 INTEGER,
col6 INTEGER
)Above, the CreateTable construct works like any
other expression construct (such as select(), table.insert(), etc.).
All of SQLAlchemy’s DDL oriented constructs are subclasses of
the DDLElement base class; this is the base of all the
objects corresponding to CREATE and DROP as well as ALTER,
not only in SQLAlchemy but in Alembic Migrations as well.
A full reference of available constructs is in DDL Expression Constructs API.
User-defined DDL constructs may also be created as subclasses of
DDLElement itself. The documentation in
Custom SQL Constructs and Compilation Extension has several examples of this.
The event-driven DDL system described in the previous section
Controlling DDL Sequences is available with other DDLElement
objects as well. However, when dealing with the built-in constructs
such as CreateIndex, CreateSequence, etc, the event
system is of limited use, as methods like Table.create() and
MetaData.create_all() will invoke these constructs unconditionally.
In a future SQLAlchemy release, the DDL event system including conditional
execution will taken into account for built-in constructs that currently
invoke in all cases.
We can illustrate an event-driven
example with the AddConstraint and DropConstraint
constructs, as the event-driven system will work for CHECK and UNIQUE
constraints, using these as we did in our previous example of
DDLElement.execute_if():
def should_create(ddl, target, connection, **kw):
row = connection.execute(
"select conname from pg_constraint where conname='%s'" %
ddl.element.name).scalar()
return not bool(row)
def should_drop(ddl, target, connection, **kw):
return not should_create(ddl, target, connection, **kw)
event.listen(
users,
"after_create",
AddConstraint(constraint).execute_if(callable_=should_create)
)
event.listen(
users,
"before_drop",
DropConstraint(constraint).execute_if(callable_=should_drop)
)
sqlusers.create(engine)
CREATE TABLE users (
user_id SERIAL NOT NULL,
user_name VARCHAR(40) NOT NULL,
PRIMARY KEY (user_id)
)
select conname from pg_constraint where conname='cst_user_name_length'
ALTER TABLE users ADD CONSTRAINT cst_user_name_length CHECK (length(user_name) >= 8)
sqlusers.drop(engine)
select conname from pg_constraint where conname='cst_user_name_length'
ALTER TABLE users DROP CONSTRAINT cst_user_name_length
DROP TABLE usersWhile the above example is against the built-in AddConstraint
and DropConstraint objects, the main usefulness of DDL events
for now remains focused on the use of the DDL construct itself,
as well as with user-defined subclasses of DDLElement that aren’t
already part of the MetaData.create_all(), Table.create(),
and corresponding “drop” processes.
DDL Expression Constructs API¶
-
sqlalchemy.schema.sort_tables(tables, skip_fn=None, extra_dependencies=None)¶ sort a collection of
Tableobjects based on dependency.This is a dependency-ordered sort which will emit
Tableobjects such that they will follow their dependentTableobjects. Tables are dependent on another based on the presence ofForeignKeyConstraintobjects as well as explicit dependencies added byTable.add_is_dependent_on().경고
The
sort_tables()function cannot by itself accommodate automatic resolution of dependency cycles between tables, which are usually caused by mutually dependent foreign key constraints. To resolve these cycles, either theForeignKeyConstraint.use_alterparameter may be appled to those constraints, or use thesql.sort_tables_and_constraints()function which will break out foreign key constraints involved in cycles separately.매개 변수: - tables¶ – a sequence of
Tableobjects. - skip_fn¶ – optional callable which will be passed a
ForeignKeyobject; if it returns True, this constraint will not be considered as a dependency. Note this is different from the same parameter insort_tables_and_constraints(), which is instead passed the owningForeignKeyConstraintobject. - extra_dependencies¶ – a sequence of 2-tuples of tables which will also be considered as dependent on each other.
- tables¶ – a sequence of
-
sqlalchemy.schema.sort_tables_and_constraints(tables, filter_fn=None, extra_dependencies=None)¶ sort a collection of
Table/ForeignKeyConstraintobjects.This is a dependency-ordered sort which will emit tuples of
(Table, [ForeignKeyConstraint, ...])such that eachTablefollows its dependentTableobjects. RemainingForeignKeyConstraintobjects that are separate due to dependency rules not satisifed by the sort are emitted afterwards as(None, [ForeignKeyConstraint ...]).Tables are dependent on another based on the presence of
ForeignKeyConstraintobjects, explicit dependencies added byTable.add_is_dependent_on(), as well as dependencies stated here using theskip_fnand/orextra_dependenciesparameters.매개 변수: - tables¶ – a sequence of
Tableobjects. - filter_fn¶ – optional callable which will be passed a
ForeignKeyConstraintobject, and returns a value based on whether this constraint should definitely be included or excluded as an inline constraint, or neither. If it returns False, the constraint will definitely be included as a dependency that cannot be subject to ALTER; if True, it will only be included as an ALTER result at the end. Returning None means the constraint is included in the table-based result unless it is detected as part of a dependency cycle. - extra_dependencies¶ – a sequence of 2-tuples of tables which will also be considered as dependent on each other.
버전 1.0.0에 추가.
더 보기
- tables¶ – a sequence of
-
class
sqlalchemy.schema.DDLElement¶ Bases:
sqlalchemy.sql.expression.Executable,sqlalchemy.schema._DDLCompilesBase class for DDL expression constructs.
This class is the base for the general purpose
DDLclass, as well as the various create/drop clause constructs such asCreateTable,DropTable,AddConstraint, etc.DDLElementintegrates closely with SQLAlchemy events, introduced in Events. An instance of one is itself an event receiving callable:event.listen( users, 'after_create', AddConstraint(constraint).execute_if(dialect='postgresql') )
-
__call__(target, bind, **kw)¶ Execute the DDL as a ddl_listener.
-
against(target)¶ Return a copy of this DDL against a specific schema item.
-
bind¶
-
callable_= None¶
-
dialect= None¶
-
execute(bind=None, target=None)¶ Execute this DDL immediately.
Executes the DDL statement in isolation using the supplied
ConnectableorConnectableassigned to the.bindproperty, if not supplied. If the DDL has a conditionaloncriteria, it will be invoked with None as the event.매개 변수: - bind¶ – Optional, an
EngineorConnection. If not supplied, a validConnectablemust be present in the.bindproperty. - target¶ – Optional, defaults to None. The target SchemaItem for the
execute call. Will be passed to the
oncallable if any, and may also provide string expansion data for the statement. Seeexecute_atfor more information.
- bind¶ – Optional, an
-
execute_at(event_name, target)¶ Link execution of this DDL to the DDL lifecycle of a SchemaItem.
버전 0.7 폐지: See
DDLEvents, as well asDDLElement.execute_if().Links this
DDLElementto aTableorMetaDatainstance, executing it when that schema item is created or dropped. The DDL statement will be executed using the same Connection and transactional context as the Table create/drop itself. The.bindproperty of this statement is ignored.매개 변수: A DDLElement instance can be linked to any number of schema items.
execute_atbuilds on theappend_ddl_listenerinterface ofMetaDataandTableobjects.Caveat: Creating or dropping a Table in isolation will also trigger any DDL set to
execute_atthat Table’s MetaData. This may change in a future release.
-
execute_if(dialect=None, callable_=None, state=None)¶ Return a callable that will execute this DDLElement conditionally.
Used to provide a wrapper for event listening:
event.listen( metadata, 'before_create', DDL("my_ddl").execute_if(dialect='postgresql') )
매개 변수: - dialect¶ –
May be a string, tuple or a callable predicate. If a string, it will be compared to the name of the executing database dialect:
DDL('something').execute_if(dialect='postgresql')
If a tuple, specifies multiple dialect names:
DDL('something').execute_if(dialect=('postgresql', 'mysql'))
- callable_¶ –
A callable, which will be invoked with four positional arguments as well as optional keyword arguments:
ddl: This DDL element. target: The TableorMetaDataobject which is the target of this event. May be None if the DDL is executed explicitly.bind: The Connectionbeing used for DDL executiontables: Optional keyword argument - a list of Table objects which are to be created/ dropped within a MetaData.create_all() or drop_all() method call. state: Optional keyword argument - will be the stateargument passed to this function.checkfirst: Keyword argument, will be True if the ‘checkfirst’ flag was set during the call to create(),create_all(),drop(),drop_all().If the callable returns a true value, the DDL statement will be executed.
- state¶ – any value which will be passed to the callable_
as the
statekeyword argument.
- dialect¶ –
-
on= None¶
-
target= None¶
-
-
class
sqlalchemy.schema.DDL(statement, on=None, context=None, bind=None)¶ Bases:
sqlalchemy.schema.DDLElementA literal DDL statement.
Specifies literal SQL DDL to be executed by the database. DDL objects function as DDL event listeners, and can be subscribed to those events listed in
DDLEvents, using eitherTableorMetaDataobjects as targets. Basic templating support allows a single DDL instance to handle repetitive tasks for multiple tables.Examples:
from sqlalchemy import event, DDL tbl = Table('users', metadata, Column('uid', Integer)) event.listen(tbl, 'before_create', DDL('DROP TRIGGER users_trigger')) spow = DDL('ALTER TABLE %(table)s SET secretpowers TRUE') event.listen(tbl, 'after_create', spow.execute_if(dialect='somedb')) drop_spow = DDL('ALTER TABLE users SET secretpowers FALSE') connection.execute(drop_spow)
When operating on Table events, the following
statementstring substitions are available:%(table)s - the Table name, with any required quoting applied %(schema)s - the schema name, with any required quoting applied %(fullname)s - the Table name including schema, quoted if needed
The DDL’s “context”, if any, will be combined with the standard substitutions noted above. Keys present in the context will override the standard substitutions.
-
__init__(statement, on=None, context=None, bind=None)¶ Create a DDL statement.
매개 변수: - statement¶ –
A string or unicode string to be executed. Statements will be processed with Python’s string formatting operator. See the
contextargument and theexecute_atmethod.A literal ‘%’ in a statement must be escaped as ‘%%’.
SQL bind parameters are not available in DDL statements.
- on¶ –
버전 0.7 폐지: See
DDLElement.execute_if().Optional filtering criteria. May be a string, tuple or a callable predicate. If a string, it will be compared to the name of the executing database dialect:
DDL('something', on='postgresql')
If a tuple, specifies multiple dialect names:
DDL('something', on=('postgresql', 'mysql'))
If a callable, it will be invoked with four positional arguments as well as optional keyword arguments:
ddl: This DDL element. event: The name of the event that has triggered this DDL, such as ‘after-create’ Will be None if the DDL is executed explicitly. target: The TableorMetaDataobject which is the target of this event. May be None if the DDL is executed explicitly.connection: The Connectionbeing used for DDL executiontables: Optional keyword argument - a list of Table objects which are to be created/ dropped within a MetaData.create_all() or drop_all() method call. If the callable returns a true value, the DDL statement will be executed.
- context¶ – Optional dictionary, defaults to None. These values will be available for use in string substitutions on the DDL statement.
- bind¶ – Optional. A
Connectable, used by default whenexecute()is invoked without a bind argument.
- statement¶ –
-
-
class
sqlalchemy.schema._CreateDropBase(element, on=None, bind=None)¶ Bases:
sqlalchemy.schema.DDLElementBase class for DDL constructs that represent CREATE and DROP or equivalents.
The common theme of _CreateDropBase is a single
elementattribute which refers to the element to be created or dropped.
-
class
sqlalchemy.schema.CreateTable(element, on=None, bind=None, include_foreign_key_constraints=None)¶ Bases:
sqlalchemy.schema._CreateDropBaseRepresent a CREATE TABLE statement.
-
__init__(element, on=None, bind=None, include_foreign_key_constraints=None)¶ Create a
CreateTableconstruct.매개 변수: - element¶ – a
Tablethat’s the subject of the CREATE - on¶ – See the description for ‘on’ in
DDL. - bind¶ – See the description for ‘bind’ in
DDL. - include_foreign_key_constraints¶ –
optional sequence of
ForeignKeyConstraintobjects that will be included inline within the CREATE construct; if omitted, all foreign key constraints that do not specify use_alter=True are included.버전 1.0.0에 추가.
- element¶ – a
-
-
class
sqlalchemy.schema.DropTable(element, on=None, bind=None)¶ Bases:
sqlalchemy.schema._CreateDropBaseRepresent a DROP TABLE statement.
-
class
sqlalchemy.schema.CreateColumn(element)¶ Bases:
sqlalchemy.schema._DDLCompilesRepresent a
Columnas rendered in a CREATE TABLE statement, via theCreateTableconstruct.This is provided to support custom column DDL within the generation of CREATE TABLE statements, by using the compiler extension documented in Custom SQL Constructs and Compilation Extension to extend
CreateColumn.Typical integration is to examine the incoming
Columnobject, and to redirect compilation if a particular flag or condition is found:from sqlalchemy import schema from sqlalchemy.ext.compiler import compiles @compiles(schema.CreateColumn) def compile(element, compiler, **kw): column = element.element if "special" not in column.info: return compiler.visit_create_column(element, **kw) text = "%s SPECIAL DIRECTIVE %s" % ( column.name, compiler.type_compiler.process(column.type) ) default = compiler.get_column_default_string(column) if default is not None: text += " DEFAULT " + default if not column.nullable: text += " NOT NULL" if column.constraints: text += " ".join( compiler.process(const) for const in column.constraints) return text
The above construct can be applied to a
Tableas follows:from sqlalchemy import Table, Metadata, Column, Integer, String from sqlalchemy import schema metadata = MetaData() table = Table('mytable', MetaData(), Column('x', Integer, info={"special":True}, primary_key=True), Column('y', String(50)), Column('z', String(20), info={"special":True}) ) metadata.create_all(conn)
Above, the directives we’ve added to the
Column.infocollection will be detected by our custom compilation scheme:CREATE TABLE mytable ( x SPECIAL DIRECTIVE INTEGER NOT NULL, y VARCHAR(50), z SPECIAL DIRECTIVE VARCHAR(20), PRIMARY KEY (x) )The
CreateColumnconstruct can also be used to skip certain columns when producing aCREATE TABLE. This is accomplished by creating a compilation rule that conditionally returnsNone. This is essentially how to produce the same effect as using thesystem=Trueargument onColumn, which marks a column as an implicitly-present “system” column.For example, suppose we wish to produce a
Tablewhich skips rendering of the Postgresqlxmincolumn against the Postgresql backend, but on other backends does render it, in anticipation of a triggered rule. A conditional compilation rule could skip this name only on Postgresql:from sqlalchemy.schema import CreateColumn @compiles(CreateColumn, "postgresql") def skip_xmin(element, compiler, **kw): if element.element.name == 'xmin': return None else: return compiler.visit_create_column(element, **kw) my_table = Table('mytable', metadata, Column('id', Integer, primary_key=True), Column('xmin', Integer) )
Above, a
CreateTableconstruct will generate aCREATE TABLEwhich only includes theidcolumn in the string; thexmincolumn will be omitted, but only against the Postgresql backend.버전 0.8.3에 추가: The
CreateColumnconstruct supports skipping of columns by returningNonefrom a custom compilation rule.버전 0.8에 추가: The
CreateColumnconstruct was added to support custom column creation styles.
-
class
sqlalchemy.schema.CreateSequence(element, on=None, bind=None)¶ Bases:
sqlalchemy.schema._CreateDropBaseRepresent a CREATE SEQUENCE statement.
-
class
sqlalchemy.schema.DropSequence(element, on=None, bind=None)¶ Bases:
sqlalchemy.schema._CreateDropBaseRepresent a DROP SEQUENCE statement.
-
class
sqlalchemy.schema.CreateIndex(element, on=None, bind=None)¶ Bases:
sqlalchemy.schema._CreateDropBaseRepresent a CREATE INDEX statement.
-
class
sqlalchemy.schema.DropIndex(element, on=None, bind=None)¶ Bases:
sqlalchemy.schema._CreateDropBaseRepresent a DROP INDEX statement.
-
class
sqlalchemy.schema.AddConstraint(element, *args, **kw)¶ Bases:
sqlalchemy.schema._CreateDropBaseRepresent an ALTER TABLE ADD CONSTRAINT statement.
-
class
sqlalchemy.schema.DropConstraint(element, cascade=False, **kw)¶ Bases:
sqlalchemy.schema._CreateDropBaseRepresent an ALTER TABLE DROP CONSTRAINT statement.
-
class
sqlalchemy.schema.CreateSchema(name, quote=None, **kw)¶ Bases:
sqlalchemy.schema._CreateDropBaseRepresent a CREATE SCHEMA statement.
버전 0.7.4에 추가.
The argument here is the string name of the schema.
-
__init__(name, quote=None, **kw)¶ Create a new
CreateSchemaconstruct.
-
-
class
sqlalchemy.schema.DropSchema(name, quote=None, cascade=False, **kw)¶ Bases:
sqlalchemy.schema._CreateDropBaseRepresent a DROP SCHEMA statement.
The argument here is the string name of the schema.
버전 0.7.4에 추가.
-
__init__(name, quote=None, cascade=False, **kw)¶ Create a new
DropSchemaconstruct.
-
