MetaData / Schema

My program is hanging when I say table.drop() / metadata.drop_all()

This usually corresponds to two conditions: 1. using PostgreSQL, which is really strict about table locks, and 2. you have a connection still open which contains locks on the table and is distinct from the connection being used for the DROP statement. Heres the most minimal version of the pattern:

  1. connection = engine.connect()
  2. result = connection.execute(mytable.select())
  3. mytable.drop(engine)

Above, a connection pool connection is still checked out; furthermore, the result object above also maintains a link to this connection. If “implicit execution” is used, the result will hold this connection opened until the result object is closed or all rows are exhausted.

The call to mytable.drop(engine) attempts to emit DROP TABLE on a second connection procured from the Engine which will lock.

The solution is to close out all connections before emitting DROP TABLE:

  1. connection = engine.connect()
  2. result = connection.execute(mytable.select())
  3. # fully read result sets
  4. result.fetchall()
  5. # close connections
  6. connection.close()
  7. # now locks are removed
  8. mytable.drop(engine)

Does SQLAlchemy support ALTER TABLE, CREATE VIEW, CREATE TRIGGER, Schema Upgrade Functionality?

General ALTER support isn’t present in SQLAlchemy directly. For special DDL on an ad-hoc basis, the DDL and related constructs can be used. See Customizing DDL for a discussion on this subject.

A more comprehensive option is to use schema migration tools, such as Alembic or SQLAlchemy-Migrate; see Altering Database Objects through Migrations for discussion on this.

How can I sort Table objects in order of their dependency?

This is available via the MetaData.sorted_tables function:

  1. metadata = MetaData()
  2. # ... add Table objects to metadata
  3. ti = metadata.sorted_tables:
  4. for t in ti:
  5. print(t)

How can I get the CREATE TABLE/ DROP TABLE output as a string?

Modern SQLAlchemy has clause constructs which represent DDL operations. These can be rendered to strings like any other SQL expression:

  1. from sqlalchemy.schema import CreateTable
  2. print(CreateTable(mytable))

To get the string specific to a certain engine:

  1. print(CreateTable(mytable).compile(engine))

There’s also a special form of Engine available via create_mock_engine() that allows one to dump an entire metadata creation sequence as a string, using this recipe:

  1. from sqlalchemy import create_mock_engine
  2. def dump(sql, *multiparams, **params):
  3. print(sql.compile(dialect=engine.dialect))
  4. engine = create_mock_engine('postgresql://', dump)
  5. metadata.create_all(engine, checkfirst=False)

The Alembic tool also supports an “offline” SQL generation mode that renders database migrations as SQL scripts.

How can I subclass Table/Column to provide certain behaviors/configurations?

Table and Column are not good targets for direct subclassing. However, there are simple ways to get on-construction behaviors using creation functions, and behaviors related to the linkages between schema objects such as constraint conventions or naming conventions using attachment events. An example of many of these techniques can be seen at Naming Conventions.