1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
.. _metadata_ddl_toplevel:
.. _metadata_ddl:
.. currentmodule:: sqlalchemy.schema
Customizing DDL
===============
In the preceding sections we've discussed a variety of schema constructs
including :class:`~sqlalchemy.schema.Table`,
:class:`~sqlalchemy.schema.ForeignKeyConstraint`,
:class:`~sqlalchemy.schema.CheckConstraint`, and
:class:`~sqlalchemy.schema.Sequence`. Throughout, we've relied upon the
``create()`` and :func:`~sqlalchemy.schema.MetaData.create_all` methods of
:class:`~sqlalchemy.schema.Table` and :class:`~sqlalchemy.schema.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
:class:`~sqlalchemy.schema.DDL` construct. This construct works like all the
other DDL elements except it accepts a string which is the text to be emitted:
.. sourcecode:: python+sql
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 :ref:`sqlalchemy.ext.compiler_toplevel` for
details.
.. _schema_ddl_sequences:
Controlling DDL Sequences
-------------------------
The :class:`_schema.DDL` construct introduced previously also has the
ability to be invoked conditionally based on inspection of the
database. This feature is available using the :meth:`.ExecutableDDLElement.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)),
)
func = DDL(
"CREATE FUNCTION my_func() "
"RETURNS TRIGGER AS $$ "
"BEGIN "
"NEW.data := 'ins'; "
"RETURN NEW; "
"END; $$ LANGUAGE PLPGSQL"
)
trigger = DDL(
"CREATE TRIGGER dt_ins BEFORE INSERT ON mytable "
"FOR EACH ROW EXECUTE PROCEDURE my_func();"
)
event.listen(mytable, "after_create", func.execute_if(dialect="postgresql"))
event.listen(mytable, "after_create", trigger.execute_if(dialect="postgresql"))
The :paramref:`.ExecutableDDLElement.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 :meth:`.ExecutableDDLElement.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:
.. sourcecode:: python+sql
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
),
)
users.create(engine)
{execsql}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)
{stop}
users.drop(engine)
{execsql}SELECT conname FROM pg_constraint WHERE conname='cst_user_name_length'
ALTER TABLE users DROP CONSTRAINT cst_user_name_length
DROP TABLE users{stop}
Using the built-in DDLElement Classes
-------------------------------------
The ``sqlalchemy.schema`` package contains SQL expression constructs that
provide DDL expressions, all of which extend from the common base
:class:`.ExecutableDDLElement`. For example, to produce a ``CREATE TABLE`` statement,
one can use the :class:`.CreateTable` construct:
.. sourcecode:: python+sql
from sqlalchemy.schema import CreateTable
with engine.connect() as conn:
conn.execute(CreateTable(mytable))
{execsql}CREATE TABLE mytable (
col1 INTEGER,
col2 INTEGER,
col3 INTEGER,
col4 INTEGER,
col5 INTEGER,
col6 INTEGER
){stop}
Above, the :class:`~sqlalchemy.schema.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 :class:`.ExecutableDDLElement` 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 :ref:`schema_api_ddl`.
User-defined DDL constructs may also be created as subclasses of
:class:`.ExecutableDDLElement` itself. The documentation in
:ref:`sqlalchemy.ext.compiler_toplevel` has several examples of this.
.. _schema_ddl_ddl_if:
Controlling DDL Generation of Constraints and Indexes
-----------------------------------------------------
.. versionadded:: 2.0
While the previously mentioned :meth:`.ExecutableDDLElement.execute_if` method is
useful for custom :class:`.DDL` classes which need to invoke conditionally,
there is also a common need for elements that are typically related to a
particular :class:`.Table`, namely constraints and indexes, to also be
subject to "conditional" rules, such as an index that includes features
that are specific to a particular backend such as PostgreSQL or SQL Server.
For this use case, the :meth:`.Constraint.ddl_if` and :meth:`.Index.ddl_if`
methods may be used against constructs such as :class:`.CheckConstraint`,
:class:`.UniqueConstraint` and :class:`.Index`, accepting the same
arguments as the :meth:`.ExecutableDDLElement.execute_if` method in order to control
whether or not their DDL will be emitted in terms of their parent
:class:`.Table` object. These methods may be used inline when
creating the definition for a :class:`.Table`
(or similarly, when using the ``__table_args__`` collection in an ORM
declarative mapping), such as::
from sqlalchemy import CheckConstraint, Index
from sqlalchemy import MetaData, Table, Column
from sqlalchemy import Integer, String
meta = MetaData()
my_table = Table(
"my_table",
meta,
Column("id", Integer, primary_key=True),
Column("num", Integer),
Column("data", String),
Index("my_pg_index", "data").ddl_if(dialect="postgresql"),
CheckConstraint("num > 5").ddl_if(dialect="postgresql"),
)
In the above example, the :class:`.Table` construct refers to both an
:class:`.Index` and a :class:`.CheckConstraint` construct, both which
indicate ``.ddl_if(dialect="postgresql")``, which indicates that these
elements will be included in the CREATE TABLE sequence only against the
PostgreSQL dialect. If we run ``meta.create_all()`` against the SQLite
dialect, for example, neither construct will be included:
.. sourcecode:: pycon+sql
>>> from sqlalchemy import create_engine
>>> sqlite_engine = create_engine("sqlite+pysqlite://", echo=True)
>>> meta.create_all(sqlite_engine)
{execsql}BEGIN (implicit)
PRAGMA main.table_info("my_table")
[raw sql] ()
PRAGMA temp.table_info("my_table")
[raw sql] ()
CREATE TABLE my_table (
id INTEGER NOT NULL,
num INTEGER,
data VARCHAR,
PRIMARY KEY (id)
)
However, if we run the same commands against a PostgreSQL database, we will
see inline DDL for the CHECK constraint as well as a separate CREATE
statement emitted for the index:
.. sourcecode:: pycon+sql
>>> from sqlalchemy import create_engine
>>> postgresql_engine = create_engine(
... "postgresql+psycopg2://scott:tiger@localhost/test", echo=True
... )
>>> meta.create_all(postgresql_engine)
{execsql}BEGIN (implicit)
select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where pg_catalog.pg_table_is_visible(c.oid) and relname=%(name)s
[generated in 0.00009s] {'name': 'my_table'}
CREATE TABLE my_table (
id SERIAL NOT NULL,
num INTEGER,
data VARCHAR,
PRIMARY KEY (id),
CHECK (num > 5)
)
[no key 0.00007s] {}
CREATE INDEX my_pg_index ON my_table (data)
[no key 0.00013s] {}
COMMIT
The :meth:`.Constraint.ddl_if` and :meth:`.Index.ddl_if` methods create
an event hook that may be consulted not just at DDL execution time, as is the
behavior with :meth:`.ExecutableDDLElement.execute_if`, but also within the SQL compilation
phase of the :class:`.CreateTable` object, which is responsible for rendering
the ``CHECK (num > 5)`` DDL inline within the CREATE TABLE statement.
As such, the event hook that is received by the :meth:`.Constraint.ddl_if.callable_`
parameter has a richer argument set present, including that there is
a ``dialect`` keyword argument passed, as well as an instance of :class:`.DDLCompiler`
via the ``compiler`` keyword argument for the "inline rendering" portion of the
sequence. The ``bind`` argument is **not** present when the event is triggered
within the :class:`.DDLCompiler` sequence, so a modern event hook that wishes
to inspect the database versioning information would best use the given
:class:`.Dialect` object, such as to test PostgreSQL versioning:
.. sourcecode:: python+sql
def only_pg_14(ddl_element, target, bind, dialect, **kw):
return dialect.name == "postgresql" and dialect.server_version_info >= (14,)
my_table = Table(
"my_table",
meta,
Column("id", Integer, primary_key=True),
Column("num", Integer),
Column("data", String),
Index("my_pg_index", "data").ddl_if(callable_=only_pg_14),
)
.. seealso::
:meth:`.Constraint.ddl_if`
:meth:`.Index.ddl_if`
.. _schema_api_ddl:
DDL Expression Constructs API
-----------------------------
.. autofunction:: sort_tables
.. autofunction:: sort_tables_and_constraints
.. autoclass:: BaseDDLElement
:members:
.. autoclass:: ExecutableDDLElement
:members:
.. autoclass:: DDL
:members:
.. autoclass:: _CreateDropBase
.. autoclass:: CreateTable
:members:
.. autoclass:: DropTable
:members:
.. autoclass:: CreateColumn
:members:
.. autoclass:: CreateSequence
:members:
.. autoclass:: DropSequence
:members:
.. autoclass:: CreateIndex
:members:
.. autoclass:: DropIndex
:members:
.. autoclass:: AddConstraint
:members:
.. autoclass:: DropConstraint
:members:
.. autoclass:: CreateSchema
:members:
.. autoclass:: DropSchema
:members:
|