This section discusses the fundamental Table
, Column
and MetaData
objects.
A collection of metadata entities is stored in an object aptly named
MetaData
:
from sqlalchemy import *
metadata = MetaData()
MetaData
is a container object that keeps together
many different features of a database (or multiple databases) being described.
To represent a table, use the Table
class. Its two
primary arguments are the table name, then the
MetaData
object which it will be associated with.
The remaining positional arguments are mostly
Column
objects describing each column:
user = Table('user', metadata,
Column('user_id', Integer, primary_key = True),
Column('user_name', String(16), nullable = False),
Column('email_address', String(60)),
Column('password', String(20), nullable = False)
)
Above, a table called user
is described, which contains four columns. The
primary key of the table consists of the user_id
column. Multiple columns
may be assigned the primary_key=True
flag which denotes a multi-column
primary key, known as a composite primary key.
Note also that each column describes its datatype using objects corresponding
to genericized types, such as Integer
and
String
. SQLAlchemy features dozens of types of
varying levels of specificity as well as the ability to create custom types.
Documentation on the type system can be found at types.
The MetaData
object contains all of the schema
constructs we’ve associated with it. It supports a few methods of accessing
these table objects, such as the sorted_tables
accessor which returns a
list of each Table
object in order of foreign key
dependency (that is, each table is preceded by all tables which it
references):
>>> for t in metadata.sorted_tables:
... print t.name
user
user_preference
invoice
invoice_item
In most cases, individual Table
objects have been
explicitly declared, and these objects are typically accessed directly as
module-level variables in an application. Once a
Table
has been defined, it has a full set of
accessors which allow inspection of its properties. Given the following
Table
definition:
employees = Table('employees', metadata,
Column('employee_id', Integer, primary_key=True),
Column('employee_name', String(60), nullable=False),
Column('employee_dept', Integer, ForeignKey("departments.department_id"))
)
Note the ForeignKey
object used in this table -
this construct defines a reference to a remote table, and is fully described
in metadata_foreignkeys. Methods of accessing information about this
table include:
# access the column "EMPLOYEE_ID":
employees.columns.employee_id
# or just
employees.c.employee_id
# via string
employees.c['employee_id']
# iterate through all columns
for c in employees.c:
print c
# get the table's primary key columns
for primary_key in employees.primary_key:
print primary_key
# get the table's foreign key objects:
for fkey in employees.foreign_keys:
print fkey
# access the table's MetaData:
employees.metadata
# access the table's bound Engine or Connection, if its MetaData is bound:
employees.bind
# access a column's name, type, nullable, primary key, foreign key
employees.c.employee_id.name
employees.c.employee_id.type
employees.c.employee_id.nullable
employees.c.employee_id.primary_key
employees.c.employee_dept.foreign_keys
# get the "key" of a column, which defaults to its name, but can
# be any user-defined string:
employees.c.employee_name.key
# access a column's table:
employees.c.employee_id.table is employees
# get the table related by a foreign key
list(employees.c.employee_dept.foreign_keys)[0].column.table
Once you’ve defined some Table
objects, assuming
you’re working with a brand new database one thing you might want to do is
issue CREATE statements for those tables and their related constructs (as an
aside, it’s also quite possible that you don’t want to do this, if you
already have some preferred methodology such as tools included with your
database or an existing scripting system - if that’s the case, feel free to
skip this section - SQLAlchemy has no requirement that it be used to create
your tables).
The usual way to issue CREATE is to use
create_all()
on the
MetaData
object. This method will issue queries
that first check for the existence of each individual table, and if not found
will issue the CREATE statements:
engine = create_engine('sqlite:///:memory:') metadata = MetaData() user = Table('user', metadata, Column('user_id', Integer, primary_key = True), Column('user_name', String(16), nullable = False), Column('email_address', String(60), key='email'), Column('password', String(20), nullable = False) ) user_prefs = Table('user_prefs', metadata, Column('pref_id', Integer, primary_key=True), Column('user_id', Integer, ForeignKey("user.user_id"), nullable=False), Column('pref_name', String(40), nullable=False), Column('pref_value', String(100)) ) sqlmetadata.create_all(engine)PRAGMA table_info(user){} CREATE TABLE user( user_id INTEGER NOT NULL PRIMARY KEY, user_name VARCHAR(16) NOT NULL, email_address VARCHAR(60), password VARCHAR(20) NOT NULL ) PRAGMA table_info(user_prefs){} CREATE TABLE user_prefs( pref_id INTEGER NOT NULL PRIMARY KEY, user_id INTEGER NOT NULL REFERENCES user(user_id), pref_name VARCHAR(40) NOT NULL, pref_value VARCHAR(100) )
create_all()
creates foreign key constraints
between tables usually inline with the table definition itself, and for this
reason it also generates the tables in order of their dependency. There are
options to change this behavior such that ALTER TABLE
is used instead.
Dropping all tables is similarly achieved using the
drop_all()
method. This method does the
exact opposite of create_all()
- the
presence of each table is checked first, and tables are dropped in reverse
order of dependency.
Creating and dropping individual tables can be done via the create()
and
drop()
methods of Table
. These methods by
default issue the CREATE or DROP regardless of the table being present:
engine = create_engine('sqlite:///:memory:')
meta = MetaData()
employees = Table('employees', meta,
Column('employee_id', Integer, primary_key=True),
Column('employee_name', String(60), nullable=False, key='name'),
Column('employee_dept', Integer, ForeignKey("departments.department_id"))
)
sqlemployees.create(engine)
CREATE TABLE employees(
employee_id SERIAL NOT NULL PRIMARY KEY,
employee_name VARCHAR(60) NOT NULL,
employee_dept INTEGER REFERENCES departments(department_id)
)
drop()
method:
sqlemployees.drop(engine)
DROP TABLE employee
To enable the “check first for the table existing” logic, add the
checkfirst=True
argument to create()
or drop()
:
employees.create(engine, checkfirst=True)
employees.drop(engine, checkfirst=False)
While SQLAlchemy directly supports emitting CREATE and DROP statements for schema
constructs, the ability to alter those constructs, usually via the ALTER statement
as well as other database-specific constructs, is outside of the scope of SQLAlchemy
itself. While it’s easy enough to emit ALTER statements and similar by hand,
such as by passing a string to Connection.execute()
or by using the
DDL
construct, it’s a common practice to automate the maintenance of
database schemas in relation to application code using schema migration tools.
There are two major migration tools available for SQLAlchemy:
Some databases support the concept of multiple schemas. A
Table
can reference this by specifying the
schema
keyword argument:
financial_info = Table('financial_info', meta,
Column('id', Integer, primary_key=True),
Column('value', String(100), nullable=False),
schema='remote_banks'
)
Within the MetaData
collection, this table will be
identified by the combination of financial_info
and remote_banks
. If
another table called financial_info
is referenced without the
remote_banks
schema, it will refer to a different
Table
. ForeignKey
objects can specify references to columns in this table using the form
remote_banks.financial_info.id
.
The schema
argument should be used for any name qualifiers required,
including Oracle’s “owner” attribute and similar. It also can accommodate a
dotted name for longer schemes:
schema="dbo.scott"
Table
supports database-specific options. For
example, MySQL has different table backend types, including “MyISAM” and
“InnoDB”. This can be expressed with Table
using
mysql_engine
:
addresses = Table('engine_email_addresses', meta,
Column('address_id', Integer, primary_key = True),
Column('remote_user_id', Integer, ForeignKey(users.c.user_id)),
Column('email_address', String(20)),
mysql_engine='InnoDB'
)
Other backends may support table-level options as well - these would be described in the individual documentation sections for each dialect.
sqlalchemy.schema.
Column
(*args, **kwargs)¶Bases: sqlalchemy.schema.SchemaItem
, sqlalchemy.sql.expression.ColumnClause
Represents a column in a database table.
__eq__
(other)¶__eq__()
method of ColumnOperators
Implement the ==
operator.
In a column context, produces the clause a = b
.
If the target is None
, produces a IS NULL
.
__init__
(*args, **kwargs)¶Construct a new Column
object.
Parameters: |
|
---|
__le__
(other)¶__le__()
method of ColumnOperators
Implement the <=
operator.
In a column context, produces the clause a <= b
.
__lt__
(other)¶__lt__()
method of ColumnOperators
Implement the <
operator.
In a column context, produces the clause a < b
.
__ne__
(other)¶__ne__()
method of ColumnOperators
Implement the !=
operator.
In a column context, produces the clause a != b
.
If the target is None
, produces a IS NOT NULL
.
anon_label
¶anon_label
attribute of ColumnElement
provides a constant ‘anonymous label’ for this ColumnElement.
This is a label() expression which will be named at compile time. The same label() is returned each time anon_label is called so that expressions can reference anon_label multiple times, producing the same label name at compile time.
the compiler uses this function automatically at compile time for expressions that are known to be ‘unnamed’ like binary expressions and function calls.
append_foreign_key
(fk)¶asc
()¶asc()
method of ColumnOperators
Produce a asc()
clause against the
parent object.
base_columns
¶base_columns
attribute of ColumnElement
between
(cleft, cright)¶between()
method of ColumnOperators
Produce a between()
clause against
the parent object, given the lower and upper range.
bind
= None¶collate
(collation)¶collate()
method of ColumnOperators
Produce a collate()
clause against
the parent object, given the collation string.
comparator
¶comparator
attribute of ColumnElement
compare
(other, use_proxies=False, equivalents=None, **kw)¶compare()
method of ColumnElement
Compare this ColumnElement to another.
Special arguments understood:
Parameters: |
|
---|
compile
(bind=None, dialect=None, **kw)¶compile()
method of ClauseElement
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: |
|
---|
concat
(other)¶concat()
method of ColumnOperators
Implement the ‘concat’ operator.
In a column context, produces the clause a || b
,
or uses the concat()
operator on MySQL.
contains
(other, **kwargs)¶contains()
method of ColumnOperators
Implement the ‘contains’ operator.
In a column context, produces the clause LIKE '%<other>%'
copy
(**kw)¶Create a copy of this Column
, unitialized.
This is used in Table.tometadata
.
default
= None¶desc
()¶desc()
method of ColumnOperators
Produce a desc()
clause against the
parent object.
description
¶description
attribute of ColumnClause
dispatch
¶alias of DDLEventsDispatch
distinct
()¶distinct()
method of ColumnOperators
Produce a distinct()
clause against the
parent object.
endswith
(other, **kwargs)¶endswith()
method of ColumnOperators
Implement the ‘endswith’ operator.
In a column context, produces the clause LIKE '%<other>'
expression
¶expression
attribute of ColumnElement
Return a column expression.
Part of the inspection interface; returns self.
foreign_keys
= []¶get_children
(schema_visitor=False, **kwargs)¶ilike
(other, escape=None)¶ilike()
method of ColumnOperators
Implement the ilike
operator.
In a column context, produces the clause a ILIKE other
.
E.g.:
select([sometable]).where(sometable.c.column.ilike("%foobar%"))
Parameters: |
|
---|
See also
in_
(other)¶in_()
method of ColumnOperators
Implement the in
operator.
In a column context, produces the clause a IN other
.
“other” may be a tuple/list of column expressions,
or a select()
construct.
info
¶info
attribute of SchemaItem
Info dictionary associated with the object, allowing user-defined
data to be associated with this SchemaItem
.
The dictionary is automatically generated when first accessed.
It can also be specified in the constructor of some objects,
such as Table
and Column
.
is_
(other)¶is_()
method of ColumnOperators
Implement the IS
operator.
Normally, IS
is generated automatically when comparing to a
value of None
, which resolves to NULL
. However, explicit
usage of IS
may be desirable if comparing to boolean values
on certain platforms.
New in version 0.7.9.
See also
is_clause_element
= True¶is_selectable
= False¶isnot
(other)¶isnot()
method of ColumnOperators
Implement the IS NOT
operator.
Normally, IS NOT
is generated automatically when comparing to a
value of None
, which resolves to NULL
. However, explicit
usage of IS NOT
may be desirable if comparing to boolean values
on certain platforms.
New in version 0.7.9.
See also
label
(name)¶label()
method of ColumnElement
Produce a column label, i.e. <columnname> AS <name>
.
This is a shortcut to the label()
function.
if ‘name’ is None, an anonymous label name will be generated.
like
(other, escape=None)¶like()
method of ColumnOperators
Implement the like
operator.
In a column context, produces the clause a LIKE other
.
E.g.:
select([sometable]).where(sometable.c.column.like("%foobar%"))
Parameters: |
|
---|
See also
match
(other, **kwargs)¶match()
method of ColumnOperators
Implements the ‘match’ operator.
In a column context, this produces a MATCH clause, i.e.
MATCH '<other>'
. The allowed contents of other
are database backend specific.
notilike
(other, escape=None)¶notilike()
method of ColumnOperators
implement the NOT ILIKE
operator.
This is equivalent to using negation with
ColumnOperators.ilike()
, i.e. ~x.ilike(y)
.
New in version 0.8.
See also
notin_
(other)¶notin_()
method of ColumnOperators
implement the NOT IN
operator.
This is equivalent to using negation with ColumnOperators.in_()
,
i.e. ~x.in_(y)
.
New in version 0.8.
See also
notlike
(other, escape=None)¶notlike()
method of ColumnOperators
implement the NOT LIKE
operator.
This is equivalent to using negation with
ColumnOperators.like()
, i.e. ~x.like(y)
.
New in version 0.8.
See also
nullsfirst
()¶nullsfirst()
method of ColumnOperators
Produce a nullsfirst()
clause against the
parent object.
nullslast
()¶nullslast()
method of ColumnOperators
Produce a nullslast()
clause against the
parent object.
onupdate
= None¶op
(opstring, precedence=0)¶produce a generic operator function.
e.g.:
somecolumn.op("*")(5)
produces:
somecolumn * 5
This function can also be used to make bitwise operators explicit. For example:
somecolumn.op('&')(0xff)
is a bitwise AND of the value in somecolumn
.
Parameters: |
|
---|
operate
(op, *other, **kwargs)¶operate()
method of ColumnElement
params
(*optionaldict, **kwargs)¶params()
method of Immutable
primary_key
= False¶proxy_set
¶proxy_set
attribute of ColumnElement
quote
= None¶references
(column)¶Return True if this Column references the given column via foreign key.
reverse_operate
(op, other, **kwargs)¶reverse_operate()
method of ColumnElement
self_group
(against=None)¶self_group()
method of ClauseElement
Apply a ‘grouping’ to this ClauseElement
.
This method is overridden by subclasses to return a
“grouping” construct, i.e. parenthesis. In particular
it’s used by “binary” expressions to provide a grouping
around themselves when placed into a larger expression,
as well as by select()
constructs when placed into
the FROM clause of another select()
. (Note that
subqueries should be normally created using the
Select.alias()
method, as many platforms require
nested SELECT statements to be named).
As expressions are composed together, the application of
self_group()
is automatic - end-user code should never
need to use this method directly. Note that SQLAlchemy’s
clause constructs take operator precedence into account -
so parenthesis might not be needed, for example, in
an expression like x OR (y AND z)
- AND takes precedence
over OR.
The base self_group()
method of ClauseElement
just returns self.
server_default
= None¶server_onupdate
= None¶shares_lineage
(othercolumn)¶shares_lineage()
method of ColumnElement
Return True if the given ColumnElement
has a common ancestor to this ColumnElement
.
startswith
(other, **kwargs)¶startswith()
method of ColumnOperators
Implement the startwith
operator.
In a column context, produces the clause LIKE '<other>%'
supports_execution
= False¶table
¶table
attribute of ColumnClause
timetuple
= None¶type
¶type
attribute of ColumnElement
unique_params
(*optionaldict, **kwargs)¶unique_params()
method of Immutable
sqlalchemy.schema.
MetaData
(bind=None, reflect=False, schema=None, quote_schema=None)¶Bases: sqlalchemy.schema.SchemaItem
A collection of Table
objects and their associated schema
constructs.
Holds a collection of Table
objects as well as
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.
See also
Describing Databases with MetaData - Introduction to database metadata
__init__
(bind=None, reflect=False, schema=None, quote_schema=None)¶Create a new MetaData object.
Parameters: |
|
---|
New in version 0.7.4: schema
and quote_schema
parameters.
append_ddl_listener
(event_name, listener)¶Append a DDL event listener to this MetaData
.
Deprecated. See DDLEvents
.
bind
¶An Engine
or Connection
to which this
MetaData
is bound.
Typically, a Engine
is assigned to this attribute
so that “implicit execution” may be used, or alternatively
as a means of providing engine binding information to an
ORM Session
object:
engine = create_engine("someurl://")
metadata.bind = engine
See also
Connectionless Execution, Implicit Execution - background on “bound metadata”
clear
()¶Clear all Table objects from this MetaData.
create_all
(bind=None, tables=None, checkfirst=True)¶Create all tables stored in this metadata.
Conditional by default, will not attempt to recreate tables already present in the target database.
Parameters: |
|
---|
drop_all
(bind=None, tables=None, checkfirst=True)¶Drop all tables stored in this metadata.
Conditional by default, will not attempt to drop tables not present in the target database.
Parameters: |
|
---|
is_bound
()¶True if this MetaData is bound to an Engine or Connection.
reflect
(bind=None, schema=None, views=False, only=None)¶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.
Parameters: |
|
---|
remove
(table)¶Remove the given Table object from this MetaData.
sorted_tables
¶Returns a list of Table
objects sorted in order of
foreign key dependency.
The sorting will place Table
objects that have dependencies
first, before the dependencies themselves, representing the
order in which they can be created. To get the order in which
the tables would be dropped, use the reversed()
Python built-in.
See also
Inspector.sorted_tables()
sqlalchemy.schema.
SchemaItem
¶Bases: sqlalchemy.events.SchemaEventTarget
, sqlalchemy.sql.visitors.Visitable
Base class for items that define a database schema.
get_children
(**kwargs)¶used to allow SchemaVisitor access
info
¶Info dictionary associated with the object, allowing user-defined
data to be associated with this SchemaItem
.
The dictionary is automatically generated when first accessed.
It can also be specified in the constructor of some objects,
such as Table
and Column
.
sqlalchemy.schema.
Table
(*args, **kw)¶Bases: sqlalchemy.schema.SchemaItem
, sqlalchemy.sql.expression.TableClause
Represent a table in a database.
e.g.:
mytable = Table("mytable", metadata,
Column('mytable_id', Integer, primary_key=True),
Column('value', String(50))
)
The Table
object constructs a unique instance of itself based
on its name and optional schema name within the given
MetaData
object. Calling the Table
constructor with the same name and same MetaData
argument
a second time will return the same Table
object - in this way
the Table
constructor acts as a registry function.
See also
Describing Databases with MetaData - Introduction to database metadata
Constructor arguments are as follows:
Parameters: |
|
---|
__init__
(*args, **kw)¶Constructor for Table
.
This method is a no-op. See the top-level
documentation for Table
for constructor arguments.
add_is_dependent_on
(table)¶Add a ‘dependency’ for this Table.
This is another Table object which must be created first before this one can, or dropped after this one.
Usually, dependencies between tables are determined via ForeignKey objects. However, for other situations that create dependencies outside of foreign keys (rules, inheriting), this method can manually establish such a link.
alias
(name=None)¶alias()
method of FromClause
return an alias of this FromClause
.
This is shorthand for calling:
from sqlalchemy import alias
a = alias(self, name=name)
See alias()
for details.
append_column
(column)¶Append a Column
to this Table
.
The “key” of the newly added Column
, i.e. the
value of its .key
attribute, will then be available
in the .c
collection of this Table
, and the
column definition will be included in any CREATE TABLE, SELECT,
UPDATE, etc. statements generated from this Table
construct.
Note that this does not change the definition of the table as it exists within any underlying database, assuming that table has already been created in the database. Relational databases support the addition of columns to existing tables using the SQL ALTER command, which would need to be emitted for an already-existing table that doesn’t contain the newly added column.
append_constraint
(constraint)¶Append a Constraint
to this
Table
.
This has the effect of the constraint being included in any
future CREATE TABLE statement, assuming specific DDL creation
events have not been associated with the given
Constraint
object.
Note that this does not produce the constraint within the
relational database automatically, for a table that already exists
in the database. To add a constraint to an
existing relational database table, the SQL ALTER command must
be used. SQLAlchemy also provides the
AddConstraint
construct which can produce this SQL when
invoked as an executable clause.
append_ddl_listener
(event_name, listener)¶Append a DDL event listener to this Table
.
Deprecated. See DDLEvents
.
bind
¶Return the connectable associated with this Table.
c
¶c
attribute of FromClause
An alias for the columns
attribute.
columns
¶columns
attribute of FromClause
A named-based collection of ColumnElement
objects
maintained by this FromClause
.
The columns
, or c
collection, is the gateway
to the construction of SQL expressions using table-bound or
other selectable-bound columns:
select([mytable]).where(mytable.c.somecolumn == 5)
compare
(other, **kw)¶compare()
method of ClauseElement
Compare this ClauseElement to the given ClauseElement.
Subclasses should override the default behavior, which is a straight identity comparison.
**kw are arguments consumed by subclass compare() methods and
may be used to modify the criteria for comparison.
(see ColumnElement
)
compile
(bind=None, dialect=None, **kw)¶compile()
method of ClauseElement
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: |
|
---|
correspond_on_equivalents
(column, equivalents)¶correspond_on_equivalents()
method of FromClause
Return corresponding_column for the given column, or if None search for a match in the given dictionary.
corresponding_column
(column, require_embedded=False)¶corresponding_column()
method of FromClause
Given a ColumnElement
, return the exported
ColumnElement
object from this Selectable
which corresponds to that original
Column
via a common ancestor
column.
Parameters: |
|
---|
the given ColumnElement
, if the given
ColumnElement
is actually present within a sub-element
of this FromClause
. Normally the column will match if
it merely shares a common ancestor with one of the exported
columns of this FromClause
.
count
(whereclause=None, **params)¶count()
method of TableClause
return a SELECT COUNT generated against this
TableClause
.
create
(bind=None, checkfirst=False)¶Issue a CREATE
statement for this
Table
, using the given Connectable
for connectivity.
See also
delete
(whereclause=None, **kwargs)¶delete()
method of TableClause
Generate a delete()
construct against this
TableClause
.
E.g.:
table.delete().where(table.c.id==7)
See delete()
for argument and usage information.
description
¶description
attribute of TableClause
dispatch
¶alias of DDLEventsDispatch
drop
(bind=None, checkfirst=False)¶Issue a DROP
statement for this
Table
, using the given Connectable
for connectivity.
See also
exists
(bind=None)¶Return True if this table exists.
foreign_keys
¶foreign_keys
attribute of FromClause
Return the collection of ForeignKey objects which this FromClause references.
get_children
(column_collections=True, schema_visitor=False, **kw)¶implicit_returning
= False¶info
¶info
attribute of SchemaItem
Info dictionary associated with the object, allowing user-defined
data to be associated with this SchemaItem
.
The dictionary is automatically generated when first accessed.
It can also be specified in the constructor of some objects,
such as Table
and Column
.
insert
(values=None, inline=False, **kwargs)¶insert()
method of TableClause
Generate an insert()
construct against this
TableClause
.
E.g.:
table.insert().values(name='foo')
See insert()
for argument and usage information.
is_clause_element
= True¶is_derived_from
(fromclause)¶is_derived_from()
method of FromClause
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.
is_selectable
= True¶join
(right, onclause=None, isouter=False)¶join()
method of FromClause
return a join of this FromClause
against another
FromClause
.
key
¶named_with_column
= True¶outerjoin
(right, onclause=None)¶outerjoin()
method of FromClause
return an outer join of this FromClause
against another
FromClause
.
params
(*optionaldict, **kwargs)¶params()
method of Immutable
primary_key
¶primary_key
attribute of FromClause
Return the collection of Column objects which comprise the primary key of this FromClause.
quote
= None¶replace_selectable
(old, alias)¶replace_selectable()
method of FromClause
replace all occurrences of FromClause ‘old’ with the given Alias
object, returning a copy of this FromClause
.
schema
= None¶select
(whereclause=None, **params)¶select()
method of FromClause
return a SELECT of this FromClause
.
See also
select()
- general purpose
method which allows for arbitrary column lists.
selectable
¶selectable
attribute of Selectable
self_group
(against=None)¶self_group()
method of ClauseElement
Apply a ‘grouping’ to this ClauseElement
.
This method is overridden by subclasses to return a
“grouping” construct, i.e. parenthesis. In particular
it’s used by “binary” expressions to provide a grouping
around themselves when placed into a larger expression,
as well as by select()
constructs when placed into
the FROM clause of another select()
. (Note that
subqueries should be normally created using the
Select.alias()
method, as many platforms require
nested SELECT statements to be named).
As expressions are composed together, the application of
self_group()
is automatic - end-user code should never
need to use this method directly. Note that SQLAlchemy’s
clause constructs take operator precedence into account -
so parenthesis might not be needed, for example, in
an expression like x OR (y AND z)
- AND takes precedence
over OR.
The base self_group()
method of ClauseElement
just returns self.
supports_execution
= False¶tometadata
(metadata, schema=<symbol 'retain_schema>)¶Return a copy of this Table
associated with a different
MetaData
.
E.g.:
some_engine = create_engine("sqlite:///some.db")
# create two metadata
meta1 = MetaData()
meta2 = MetaData()
# load 'users' from the sqlite engine
users_table = Table('users', meta1, autoload=True,
autoload_with=some_engine)
# create the same Table object for the plain metadata
users_table_2 = users_table.tometadata(meta2)
Parameters: |
---|
unique_params
(*optionaldict, **kwargs)¶unique_params()
method of Immutable
update
(whereclause=None, values=None, inline=False, **kwargs)¶update()
method of TableClause
Generate an update()
construct against this
TableClause
.
E.g.:
table.update().where(table.c.id==7).values(name='foo')
See update()
for argument and usage information.
sqlalchemy.schema.
ThreadLocalMetaData
¶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
.
__init__
()¶Construct a ThreadLocalMetaData.
bind
¶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()
.
dispose
()¶Dispose all bound engines, in all thread contexts.
is_bound
()¶True if there is a bind for this thread.