Here are the examples of the python api sqlalchemy.ColumnDefault taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
1 Examples
0
Example 1
Project: Flexget Source File: sqlalchemy_utils.py
def table_add_column(table, name, col_type, session, default=None):
"""Adds a column to a table
.. warning:: Uses raw statements, probably needs to be changed in
order to work on other databases besides SQLite
:param string table: Table to add column to (can be name or schema)
:param string name: Name of new column to add
:param col_type: The sqlalchemy column type to add
:param Session session: SQLAlchemy Session to do the alteration
:param default: Default value for the created column (optional)
"""
if isinstance(table, basestring):
table = table_schema(table, session)
if name in table_columns(table, session):
# If the column already exists, we don't have to do anything.
return
# Add the column to the table
if not isinstance(col_type, TypeEngine):
# If we got a type class instead of an instance of one, instantiate it
col_type = col_type()
type_string = session.bind.engine.dialect.type_compiler.process(col_type)
statement = 'ALTER TABLE %s ADD %s %s' % (table.name, name, type_string)
session.execute(statement)
# Update the table with the default value if given
if default is not None:
# Get the new schema with added column
table = table_schema(table.name, session)
if not isinstance(default, (ColumnDefault, Sequence)):
default = ColumnDefault(default)
default._set_parent(getattr(table.c, name))
statement = table.update().values({name: default.execute(bind=session.bind)})
session.execute(statement)