SQLAlchemy 0.3 Documentation
- class BoundMetaData(MetaData)
- class CheckConstraint(Constraint)
- class Column(SchemaItem,_ColumnClause)
- class ColumnDefault(DefaultGenerator)
- class Constraint(SchemaItem)
- class DefaultGenerator(SchemaItem)
- class DynamicMetaData(MetaData)
- class ForeignKey(SchemaItem)
- class ForeignKeyConstraint(Constraint)
- class Index(SchemaItem)
- class MetaData(SchemaItem)
- class PassiveDefault(DefaultGenerator)
- class PrimaryKeyConstraint(Constraint)
- class SchemaItem(object)
- class SchemaVisitor(ClauseVisitor)
- class Sequence(DefaultGenerator)
- class Table(SchemaItem,TableClause)
- class UniqueConstraint(Constraint)
module sqlalchemy.schema
The schema module provides the building blocks for database metadata.
This means all the entities within a SQL database that we might want to look at, modify, or create and delete are described by these objects, in a database-agnostic way.
A structure of SchemaItems also provides a visitor interface which is the primary method by which other methods operate upon the schema. The SQL package extends this structure with its own clause-specific objects as well as the visitor interface, so that the schema package plugs in to the SQL package.
class BoundMetaData(MetaData)
Build upon MetaData to provide the capability to bind to an Engine implementation.
class CheckConstraint(Constraint)
back to section topclass Column(SchemaItem,_ColumnClause)
Represent a column in a database table.
This is a subclass of sql.ColumnClause and represents an actual existing table in the database, in a similar fashion as TableClause/Table.
Construct a new Column object.
Arguments are:
- name
- The name of this column. This should be the identical name as it appears, or will appear, in the database.
- type
- The TypeEngine for this column. This can be any subclass of types.AbstractType, including the database-agnostic types defined in the types module, database-specific types defined within specific database modules, or user-defined types. If the column contains a ForeignKey, the type can also be None, in which case the type assigned will be that of the referenced column.
- *args
- Constraint, ForeignKey, ColumnDefault and Sequence objects should be added as list values.
- **kwargs
Keyword arguments include:
- key
- Defaults to None: an optional alias name for this column. The column will then be identified everywhere in an application, including the column list on its Table, by this key, and not the given name. Generated SQL, however, will still reference the column by its actual name.
- primary_key
- Defaults to False: True if this column is a primary key column. Multiple columns can have this flag set to specify composite primary keys. As an alternative, the primary key of a Table can be specified via an explicit PrimaryKeyConstraint instance appended to the Table's list of objects.
- nullable
- Defaults to True : True if this column should allow nulls. True is the default unless this column is a primary key column.
- default
- Defaults to None: a scalar, Python callable, or ClauseElement representing the default value for this column, which will be invoked upon insert if this column is not present in the insert list or is given a value of None. The default expression will be converted into a ColumnDefault object upon initialization.
- _is_oid
- Defaults to False: used internally to indicate that this column is used as the quasi-hidden "oid" column
- index
- Defaults to False: indicates that this column is indexed. The name of the index is autogenerated. to specify indexes with explicit names or indexes that contain multiple columns, use the Index construct instead.
- unique
- Defaults to False: indicates that this column contains a unique constraint, or if index is True as well, indicates that the Index should be created with the unique flag. To specify multiple columns in the constraint/index or to specify an explicit name, use the UniqueConstraint or Index constructs instead.
- autoincrement
- Defaults to True: indicates that integer-based primary key columns should have autoincrementing behavior, if supported by the underlying database. This will affect CREATE TABLE statements such that they will use the databases auto-incrementing keyword (such as SERIAL for Postgres, AUTO_INCREMENT for Mysql) and will also affect the behavior of some dialects during INSERT statement execution such that they will assume primary key values are created in this manner. If a Column has an explicit ColumnDefault object (such as via the default keyword, or a Sequence or PassiveDefault), then the value of autoincrement is ignored and is assumed to be False. autoincrement value is only significant for a column with a type or subtype of Integer.
- quote
- Defaults to False: indicates that the Column identifier must be properly escaped and quoted before being sent to the database. This flag should normally not be required as dialects can auto-detect conditions where quoting is required.
- case_sensitive
- Defaults to True: indicates quoting should be used if the identifier contains mixed case.
class ColumnDefault(DefaultGenerator)
A plain default value on a column.
This could correspond to a constant, a callable function, or a SQL clause.
class Constraint(SchemaItem)
Represent a table-level Constraint such as a composite primary key, foreign key, or unique constraint.
Implements a hybrid of dict/setlike behavior with regards to the list of underying columns.
class DynamicMetaData(MetaData)
Build upon MetaData to provide the capability to bind to multiple Engine implementations on a dynamically alterable, thread-local basis.
class ForeignKey(SchemaItem)
Defines a column-level ForeignKey constraint between two columns.
ForeignKey is specified as an argument to a Column object.
One or more ForeignKey objects are used within a ForeignKeyConstraint object which represents the table-level constraint definition.
Construct a new ForeignKey object.
- column
- Can be a schema.Column object representing the relationship, or just its string name given as tablename.columnname. schema can be specified as schema.tablename.columnname.
- constraint
- Is the owning ForeignKeyConstraint object, if any. if not given, then a ForeignKeyConstraint will be automatically created and added to the parent table.
class ForeignKeyConstraint(Constraint)
Table-level foreign key constraint, represents a collection of ForeignKey objects.
class Index(SchemaItem)
Represent an index of columns from a database table.
Construct an index object.
Arguments are:
- name
- The name of the index
- *columns
- Columns to include in the index. All columns must belong to the same table, and no column may appear more than once.
- **kwargs
Keyword arguments include:
- unique
- Defaults to True: create a unique index.
class MetaData(SchemaItem)
Represent a collection of Tables and their associated schema constructs.
Create all tables stored in this metadata.
This will conditionally create tables depending on if they do not yet exist in the database.
- connectable
- A Connectable used to access the database; or use the engine bound to this MetaData.
- tables
- Optional list of tables, which is a subset of the total tables in the MetaData (others are ignored).
Drop all tables stored in this metadata.
This will conditionally drop tables depending on if they currently exist in the database.
- connectable
- A Connectable used to access the database; or use the engine bound to this MetaData.
- tables
- Optional list of tables, which is a subset of the total tables in the MetaData (others are ignored).
class PassiveDefault(DefaultGenerator)
A default that takes effect on the database side.
class PrimaryKeyConstraint(Constraint)
back to section topclass Sequence(DefaultGenerator)
Represent a sequence, which applies to Oracle and Postgres databases.
class Table(SchemaItem,TableClause)
Represent a relational database table.
This subclasses sql.TableClause to provide a table that is associated with an instance of MetaData, which in turn may be associated with an instance of SQLEngine.
Whereas TableClause represents a table as its used in an SQL expression, Table represents a table as it exists in a database schema.
If this Table is ultimately associated with an engine, the Table gains the ability to access the database directly without the need for dealing with an explicit Connection object; this is known as "implicit execution".
Implicit operation allows the Table to access the database to reflect its own properties (via the autoload=True flag), it allows the create() and drop() methods to be called without passing a connectable, and it also propigates the underlying engine to constructed SQL objects so that they too can be executed via their execute() method without the need for a Connection.
Construct a Table.
Table objects can be constructed directly. The init method is actually called via the TableSingleton metaclass. Arguments are:
- name
The name of this table, exactly as it appears, or will appear, in the database.
This property, along with the schema, indicates the singleton identity of this table.
Further tables constructed with the same name/schema combination will return the same Table instance.
- *args
- Should contain a listing of the Column objects for this table.
- **kwargs
options include:
- schema
- Defaults to None: the schema name for this table, which is required if the table resides in a schema other than the default selected schema for the engine's database connection.
- autoload
- Defaults to False: the Columns for this table should be reflected from the database. Usually there will be no Column objects in the constructor if this property is set.
- mustexist
- Defaults to False: indicates that this Table must already have been defined elsewhere in the application, else an exception is raised.
- useexisting
- Defaults to False: indicates that if this Table was already defined elsewhere in the application, disregard the rest of the constructor arguments.
- owner
- Defaults to None: optional owning user of this table. useful for databases such as Oracle to aid in table reflection.
- quote
- Defaults to False: indicates that the Table identifier must be properly escaped and quoted before being sent to the database. This flag overrides all other quoting behavior.
- quote_schema
- Defaults to False: indicates that the Namespace identifier must be properly escaped and quoted before being sent to the database. This flag overrides all other quoting behavior.
- case_sensitive
- Defaults to True: indicates quoting should be used if the identifier contains mixed case.
- case_sensitive_schema
- Defaults to True: indicates quoting should be used if the identifier contains mixed case.
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 a copy of this Table associated with a different MetaData.