SQLAlchemy 0.3 Documentation

Version: 0.3.5 Last Updated: 03/18/07 18:39:07

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.

def __init__(self, engine_or_url, name=None, **kwargs)
def is_bound(self)
back to section top

class CheckConstraint(Constraint)

def __init__(self, sqltext, name=None)
def accept_visitor(self, visitor)
def copy(self)
back to section top

class 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.

def __init__(self, name, type, *args, **kwargs)

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.
def append_foreign_key(self, fk)
case_sensitive = property()
columns = property()
def copy(self)

Create a copy of this Column, unitialized.

This is used in Table.tometadata.

def get_children(self, schema_visitor=False, **kwargs)
back to section top

class ColumnDefault(DefaultGenerator)

A plain default value on a column.

This could correspond to a constant, a callable function, or a SQL clause.

def __init__(self, arg, **kwargs)
def accept_visitor(self, visitor)

Call the visit_column_default method on the given visitor.

back to section top

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.

def __init__(self, name=None)
def copy(self)
def keys(self)
back to section top

class DefaultGenerator(SchemaItem)

Base class for column default values.

def __init__(self, for_update=False, metadata=None)
def execute(self, **kwargs)
back to section top

class DynamicMetaData(MetaData)

Build upon MetaData to provide the capability to bind to multiple Engine implementations on a dynamically alterable, thread-local basis.

def __init__(self, name=None, threadlocal=True, **kwargs)
def connect(self, engine_or_url, **kwargs)
def dispose(self)

Dispose all Engines to which this DynamicMetaData has been connected.

engine = property()
def is_bound(self)
back to section top

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.

def __init__(self, column, constraint=None, use_alter=False, name=None, onupdate=None, ondelete=None)

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.
def accept_visitor(self, visitor)

Call the visit_foreign_key method on the given visitor.

column = property()
def copy(self)

Produce a copy of this ForeignKey object.

def references(self, table)

Return True if the given table is referenced by this ForeignKey.

back to section top

class ForeignKeyConstraint(Constraint)

Table-level foreign key constraint, represents a collection of ForeignKey objects.

def __init__(self, columns, refcolumns, name=None, onupdate=None, ondelete=None, use_alter=False)
def accept_visitor(self, visitor)
def append_element(self, col, refcol)
def copy(self)
back to section top

class Index(SchemaItem)

Represent an index of columns from a database table.

def __init__(self, name, *columns, **kwargs)

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.
def accept_visitor(self, visitor)
def append_column(self, column)
def create(self, connectable=None)
def drop(self, connectable=None)
back to section top

class MetaData(SchemaItem)

Represent a collection of Tables and their associated schema constructs.

def __init__(self, name=None, **kwargs)
def accept_visitor(self, visitor)
def clear(self)
def create_all(self, connectable=None, tables=None, checkfirst=True)

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).
def drop_all(self, connectable=None, tables=None, checkfirst=True)

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).
def is_bound(self)
def table_iterator(self, reverse=True, tables=None)
back to section top

class PassiveDefault(DefaultGenerator)

A default that takes effect on the database side.

def __init__(self, arg, **kwargs)
def accept_visitor(self, visitor)
back to section top

class PrimaryKeyConstraint(Constraint)

def __init__(self, *columns, **kwargs)
def accept_visitor(self, visitor)
def add(self, col)
def append_column(self, col)
def copy(self)
def remove(self, col)
back to section top

class SchemaItem(object)

Base class for items that define a database schema.

case_sensitive = property()
engine = property()
def get_children(self, **kwargs)

used to allow SchemaVisitor access

def get_engine(self)

Return the engine or raise an error if no engine.

metadata = property()
back to section top

class SchemaVisitor(ClauseVisitor)

Define the visiting for SchemaItem objects.

def visit_check_constraint(self, constraint)

Visit a CheckConstraint.

def visit_column(self, column)

Visit a Column.

def visit_column_check_constraint(self, constraint)

Visit a CheckConstraint on a Column.

def visit_column_default(self, default)

Visit a ColumnDefault.

def visit_column_onupdate(self, onupdate)

Visit a ColumnDefault with the for_update flag set.

def visit_foreign_key(self, join)

Visit a ForeignKey.

def visit_foreign_key_constraint(self, constraint)

Visit a ForeignKeyConstraint.

def visit_index(self, index)

Visit an Index.

def visit_passive_default(self, default)

Visit a passive default.

def visit_primary_key_constraint(self, constraint)

Visit a PrimaryKeyConstraint.

def visit_schema(self, schema)

Visit a generic SchemaItem.

def visit_sequence(self, sequence)

Visit a Sequence.

def visit_table(self, table)

Visit a Table.

def visit_unique_constraint(self, constraint)

Visit a UniqueConstraint.

back to section top

class Sequence(DefaultGenerator)

Represent a sequence, which applies to Oracle and Postgres databases.

def __init__(self, name, start=None, increment=None, optional=False, quote=False, **kwargs)
def accept_visitor(self, visitor)

Call the visit_seauence method on the given visitor.

def create(self)
def drop(self)
back to section top

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.

def __init__(self, name, metadata, **kwargs)

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.
def append_column(self, column)

Append a Column to this Table.

def append_constraint(self, constraint)

Append a Constraint to this Table.

case_sensitive_schema = property()
def create(self, connectable=None, checkfirst=False)

Issue a CREATE statement for this table.

See also metadata.create_all().

def drop(self, connectable=None, checkfirst=False)

Issue a DROP statement for this table.

See also metadata.drop_all().

def exists(self, connectable=None)

Return True if this table exists.

def get_children(self, column_collections=True, schema_visitor=False, **kwargs)
primary_key = property()
def tometadata(self, metadata, schema=None)

Return a copy of this Table associated with a different MetaData.

back to section top

class UniqueConstraint(Constraint)

def __init__(self, *columns, **kwargs)
def accept_visitor(self, visitor)
def append_column(self, col)
def copy(self)
back to section top