SQLAlchemy schema definition language. For more usage examples, see Database Meta Data.
Bases: sqlalchemy.schema.SchemaItem, sqlalchemy.sql.expression.ColumnClause
Represents a column in a database table.
Construct a new Column object.
Parameters: |
|
---|
Compare this ClauseElement to the given ClauseElement.
Subclasses should override the default behavior, which is a straight identity comparison.
Compile this SQL expression.
The return value is a Compiled object. Calling str() or unicode() on the returned value will yield a string representation of the result. The Compiled object also can return a dictionary of bind parameter names and values using the params accessor.
Parameters: |
|
---|
Create a copy of this Column, unitialized.
This is used in Table.tometadata.
Produce a MATCH clause, i.e. MATCH '<other>'
The allowed contents of other are database backend specific.
produce a generic operator function.
e.g.:
somecolumn.op("*")(5)
produces:
somecolumn * 5
Return a copy with bindparam() elments replaced.
Returns a copy of this ClauseElement with bindparam() elements replaced with values taken from the given dictionary:
>>> clause = column('x') + bindparam('foo')
>>> print clause.compile().params
{'foo':None}
>>> print clause.params({'foo':7}).compile().params
{'foo':7}
Return a copy with bindparam() elments replaced.
Same functionality as params(), except adds unique=True to affected bind parameters so that multiple statements can be used.
Bases: sqlalchemy.schema.SchemaItem
A collection of Tables and their associated schema constructs.
Holds a collection of Tables and an optional binding to an Engine or Connection. If bound, the Table objects in the collection and their columns may participate in implicit SQL execution.
The Table objects themselves are stored in the metadata.tables dictionary.
The bind property may be assigned to dynamically. A common pattern is to start unbound and then bind later when an engine is available:
metadata = MetaData()
# define tables
Table('mytable', metadata, ...)
# connect to an engine later, perhaps after loading a URL from a
# configuration file
metadata.bind = an_engine
MetaData is a thread-safe object after tables have been explicitly defined or loaded via reflection.
Create a new MetaData object.
Append a DDL event listener to this MetaData.
The listener callable will be triggered when this MetaData is involved in DDL creates or drops, and will be invoked either before all Table-related actions or after.
Arguments are:
A callable, invoked with three positional arguments:
Listeners are added to the MetaData’s ddl_listeners attribute.
Note: MetaData listeners are invoked even when Tables are created in isolation. This may change in a future release. I.e.:
# triggers all MetaData and Table listeners:
metadata.create_all()
# triggers MetaData listeners too:
some.table.create()
An Engine or Connection to which this MetaData is bound.
This property may be assigned an Engine or Connection, or assigned a string or URL to automatically create a basic Engine for this bind with create_engine().
Bind this MetaData to an Engine.
Deprecated. Use metadata.bind = <engine> or metadata.bind = <url>.
Create all tables stored in this metadata.
Conditional by default, will not attempt to recreate tables already present in the target database.
Drop all tables stored in this metadata.
Conditional by default, will not attempt to drop tables not present in the target database.
Load all available table definitions from the database.
Automatically creates Table entries in this MetaData for any table available in the database but not yet present in the MetaData. May be called multiple times to pick up tables recently added to the database, however no special action is taken if a table in this MetaData no longer exists in the database.
Optional. Load only a sub-set of available named tables. May be specified as a sequence of names or a callable.
If a sequence of names is provided, only those tables will be reflected. An error is raised if a table is requested but not available. Named tables already present in this MetaData are ignored.
If a callable is provided, it will be used as a boolean predicate to filter the list of potential table names. The callable is called with a table name and this MetaData instance as positional arguments and should return a true value for any table to reflect.
Deprecated - use metadata.sorted_tables().
Deprecated. Use metadata.sorted_tables
Bases: sqlalchemy.schema.SchemaItem, sqlalchemy.sql.expression.TableClause
Represent a table in a database.
Construct a Table.
Parameters: |
|
---|
return an alias of this FromClause.
For table objects, this has the effect of the table being rendered as tablename AS aliasname in a SELECT statement. For select objects, the effect is that of creating a named subquery, i.e. (select ...) AS aliasname. The alias() method is the general way to create a “subquery” out of an existing SELECT.
The name parameter is optional, and if left blank an “anonymous” name will be generated at compile time, guaranteed to be unique against other anonymous constructs used in the same statement.
Append a DDL event listener to this Table.
The listener callable will be triggered when this Table is created or dropped, either directly before or after the DDL is issued to the database. The listener may modify the Table, but may not abort the event itself.
Arguments are:
A callable, invoked with three positional arguments:
Listeners are added to the Table’s ddl_listeners attribute.
Compare this ClauseElement to the given ClauseElement.
Subclasses should override the default behavior, which is a straight identity comparison.
Compile this SQL expression.
The return value is a Compiled object. Calling str() or unicode() on the returned value will yield a string representation of the result. The Compiled object also can return a dictionary of bind parameter names and values using the params accessor.
Parameters: |
|
---|
Given a ColumnElement, return the exported ColumnElement object from this Selectable which corresponds to that original Column via a common anscestor column.
Parameters: |
|
---|
Issue a CREATE statement for this table.
See also metadata.create_all().
Issue a DROP statement for this table.
See also metadata.drop_all().
Return True if this FromClause is ‘derived’ from the given FromClause.
An example would be an Alias of a Table is derived from that Table.
Return a copy with bindparam() elments replaced.
Returns a copy of this ClauseElement with bindparam() elements replaced with values taken from the given dictionary:
>>> clause = column('x') + bindparam('foo')
>>> print clause.compile().params
{'foo':None}
>>> print clause.params({'foo':7}).compile().params
{'foo':7}
Return a copy with bindparam() elments replaced.
Same functionality as params(), except adds unique=True to affected bind parameters so that multiple statements can be used.
Bases: sqlalchemy.schema.MetaData
A MetaData variant that presents a different bind in every thread.
Makes the bind property of the MetaData a thread-local value, allowing this collection of tables to be bound to different Engine implementations or connections in each thread.
The ThreadLocalMetaData starts off bound to None in each thread. Binds must be made explicitly by assigning to the bind property or using connect(). You can also re-bind dynamically multiple times per thread, just like a regular MetaData.
The bound Engine or Connection for this thread.
This property may be assigned an Engine or Connection, or assigned a string or URL to automatically create a basic Engine for this bind with create_engine().
Bind to an Engine in the caller’s thread.
Deprecated. Use metadata.bind = <engine> or metadata.bind = <url>.
Bases: sqlalchemy.schema.Constraint
A table- or column-level CHECK constraint.
Can be included in the definition of a Table or Column.
Construct a CHECK constraint.
Bases: sqlalchemy.schema.SchemaItem
A table-level SQL constraint, such as a KEY.
Implements a hybrid of dict/setlike behavior with regards to the list of underying columns.
Create a SQL constraint.
Bases: sqlalchemy.schema.SchemaItem
Defines a column-level FOREIGN KEY constraint between two columns.
ForeignKey is specified as an argument to a Column object, e.g.:
t = Table("remote_table", metadata,
Column("remote_id", ForeignKey("main_table.id"))
)
For a composite (multiple column) FOREIGN KEY, use a ForeignKeyConstraint object specified at the level of the Table.
Further examples of foreign key configuration are in Defining Foreign Keys.
Construct a column-level FOREIGN KEY.
Parameters: |
|
---|
Return the column in the given table referenced by this ForeignKey.
Returns None if this ForeignKey does not reference the given table.
Bases: sqlalchemy.schema.Constraint
A table-level FOREIGN KEY constraint.
Defines a single column or composite FOREIGN KEY ... REFERENCES constraint. For a no-frills, single column foreign key, adding a ForeignKey to the definition of a Column is a shorthand equivalent for an unnamed, single column ForeignKeyConstraint.
Examples of foreign key configuration are in Defining Foreign Keys.
Construct a composite-capable FOREIGN KEY.
Parameters: |
|
---|
Bases: sqlalchemy.schema.SchemaItem
A table-level INDEX.
Defines a composite (one or more column) INDEX. For a no-frills, single column index, adding index=True to the Column definition is a shorthand equivalent for an unnamed, single column Index.
Construct an index object.
Arguments are:
Keyword arguments include:
Bases: sqlalchemy.schema.Constraint
A table-level PRIMARY KEY constraint.
Defines a single column or composite PRIMARY KEY constraint. For a no-frills primary key, adding primary_key=True to one or more Column definitions is a shorthand equivalent for an unnamed single- or multiple-column PrimaryKeyConstraint.
Construct a composite-capable PRIMARY KEY.
Bases: sqlalchemy.schema.Constraint
A table-level UNIQUE constraint.
Defines a single column or composite UNIQUE constraint. For a no-frills, single column constraint, adding unique=True to the Column definition is a shorthand equivalent for an unnamed, single column UniqueConstraint.
Construct a UNIQUE constraint.
Bases: sqlalchemy.schema.DefaultGenerator
A plain default value on a column.
This could correspond to a constant, a callable function, or a SQL clause.
Bases: sqlalchemy.schema.FetchedValue
A DDL-specified DEFAULT column value.
Bases: sqlalchemy.schema.SchemaItem
Base class for column default values.
Bases: object
A default that takes effect on the database side.
Bases: sqlalchemy.schema.DefaultGenerator
Represents a named database sequence.
Bases: object
A literal DDL statement.
Specifies literal SQL DDL to be executed by the database. DDL objects can be attached to Tables or MetaData instances, conditionally executing SQL as part of the DDL lifecycle of those schema items. Basic templating support allows a single DDL instance to handle repetitive tasks for multiple tables.
Examples:
tbl = Table('users', metadata, Column('uid', Integer)) # ...
DDL('DROP TRIGGER users_trigger').execute_at('before-create', tbl)
spow = DDL('ALTER TABLE %(table)s SET secretpowers TRUE', on='somedb')
spow.execute_at('after-create', tbl)
drop_spow = DDL('ALTER TABLE users SET secretpowers FALSE')
connection.execute(drop_spow)
Create a DDL statement.
A string or unicode string to be executed. Statements will be processed with Python’s string formatting operator. See the context argument and the execute_at method.
A literal ‘%’ in a statement must be escaped as ‘%%’.
SQL bind parameters are not available in DDL statements.
Optional filtering criteria. May be a string or a callable predicate. If a string, it will be compared to the name of the executing database dialect:
DDL('something', on='postgres')
If a callable, it will be invoked with three positional arguments:
- event
- The name of the event that has triggered this DDL, such as ‘after-create’ Will be None if the DDL is executed explicitly.
- schema_item
- A SchemaItem instance, such as Table or MetaData. May be None if the DDL is executed explicitly.
- connection
- The Connection being used for DDL execution
If the callable returns a true value, the DDL statement will be executed.
An Engine or Connection to which this DDL is bound.
This property may be assigned an Engine or Connection, or assigned a string or URL to automatically create a basic Engine for this bind with create_engine().
Execute this DDL immediately.
Executes the DDL statement in isolation using the supplied Connectable or Connectable assigned to the .bind property, if not supplied. If the DDL has a conditional on criteria, it will be invoked with None as the event.
Link execution of this DDL to the DDL lifecycle of a SchemaItem.
Links this DDL to a Table or MetaData instance, 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 .bind property of this statement is ignored.
When operating on Table events, the following additional statement string 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 substutions noted above. Keys present in the context will override the standard substitutions.
A DDL instance can be linked to any number of schema items. The statement subsitution support allows for DDL instances to be used in a template fashion.
execute_at builds on the append_ddl_listener interface of MetaDta and Table objects.
Caveat: Creating or dropping a Table in isolation will also trigger any DDL set to execute_at that Table’s MetaData. This may change in a future release.
Bases: sqlalchemy.sql.visitors.Visitable
Base class for items that define a database schema.
Bases: sqlalchemy.sql.visitors.ClauseVisitor
Define the visiting for SchemaItem objects.