本文整理汇总了Python中migrate.changeset.constraint.ForeignKeyConstraint类的典型用法代码示例。如果您正苦于以下问题:Python ForeignKeyConstraint类的具体用法?Python ForeignKeyConstraint怎么用?Python ForeignKeyConstraint使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ForeignKeyConstraint类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: drop_constraints_and_alter_types
def drop_constraints_and_alter_types():
# 1 drop all foreign key constraints
dropped_fk_constraints = []
revision_table = Table('revision', metadata, autoload=True)
foreign_tables = ['package', 'package_tag', 'package_revision', 'package_tag_revision', 'package_extra', 'package_extra_revision', ]
for table_name in foreign_tables:
table = Table(table_name, metadata, autoload=True)
for constraint in table.constraints.copy():
if isinstance(constraint, sqlalchemy.schema.ForeignKeyConstraint):
foreign_key_cols = [key.column for key in constraint.elements]
fk_col = foreign_key_cols[0]
if fk_col.table == revision_table:
orig_fk = ForeignKeyConstraint(constraint.columns, foreign_key_cols, name=constraint.name, table=table)
orig_fk.drop()
dropped_fk_constraints.append((constraint.columns, foreign_key_cols, constraint.name, table.name))
# 2 alter type of revision id and foreign keys
id_col = constraint.table.columns[constraint.columns[0]]
id_col.alter(type=UnicodeText)
revision_table = Table('revision', metadata, autoload=True)
id_col = revision_table.c['id']
id_col.alter(type=UnicodeText,
)
return dropped_fk_constraints
示例2: drop_constraints_and_alter_types
def drop_constraints_and_alter_types(primary_table_name, foreign_tables, revision_table_name):
# 1 drop all foreign key constraints
dropped_fk_constraints = []
primary_table = Table(primary_table_name, metadata, autoload=True)
for table_name in foreign_tables:
table = Table(table_name, metadata, autoload=True)
for constraint in table.constraints.copy():
if isinstance(constraint, sqlalchemy.schema.ForeignKeyConstraint):
foreign_key_cols = [key.column for key in constraint.elements]
fk_col = foreign_key_cols[0]
if fk_col.table == primary_table:
orig_fk = ForeignKeyConstraint(constraint.columns, foreign_key_cols, name=constraint.name, table=table)
orig_fk.drop()
dropped_fk_constraints.append((constraint.columns, foreign_key_cols, constraint.name, table.name))
#print 'CON', dropped_fk_constraints[-1]
# 2 alter type of primary table id and foreign keys
id_col = constraint.table.columns[constraint.columns[0]]
id_col.alter(type=UnicodeText)
primary_table = Table(primary_table_name, metadata, autoload=True)
id_col = primary_table.c['id']
id_col.alter(type=UnicodeText)
if revision_table_name:
# Revision table id column type changed as well
revision_table = Table(revision_table_name, metadata, autoload=True)
id_col = revision_table.c['id']
id_col.alter(type=UnicodeText)
return dropped_fk_constraints
示例3: upgrade
def upgrade(migrate_engine):
metadata = sa.MetaData()
metadata.bind = migrate_engine
builders = sautils.Table('builders', metadata, autoload=True)
masters = sautils.Table('masters', metadata, autoload=True)
workers = sautils.Table('workers', metadata, autoload=True)
builder_masters = sautils.Table('builder_masters', metadata, autoload=True)
configured_workers = sautils.Table('configured_workers', metadata,
autoload=True)
fks_to_change = []
# we need to parse the reflected model in order to find the automatic fk name that was put
# mysql and pgsql have different naming convention so this is not very easy to have generic code working.
for table, keys in [(builder_masters, (builders.c.id, masters.c.id)),
(configured_workers, (builder_masters.c.id, workers.c.id))]:
for fk in table.constraints:
if not isinstance(fk, sa.ForeignKeyConstraint):
continue
for c in fk.elements:
if c.column in keys:
# migrate.xx.ForeignKeyConstraint is changing the model so initializing here
# would break the iteration (Set changed size during iteration)
fks_to_change.append((
table, (fk.columns, [c.column]), dict(name=fk.name, ondelete='CASCADE')))
for table, args, kwargs in fks_to_change:
fk = ForeignKeyConstraint(*args, **kwargs)
table.append_constraint(fk)
try:
fk.drop()
except NotSupportedError:
pass # some versions of sqlite do not support drop, but will still update the fk
fk.create()
示例4: downgrade
def downgrade(migrate_engine):
meta = MetaData(bind=migrate_engine)
pci_devices = Table("pci_devices", meta, autoload=True)
compute_nodes = Table("compute_nodes", meta, autoload=True)
fkey = ForeignKeyConstraint(columns=[pci_devices.c.compute_node_id], refcolumns=[compute_nodes.c.id])
fkey.drop()
示例5: upgrade
def upgrade(migrate_engine):
"""Add missing foreign key constraint on pci_devices.compute_node_id."""
meta = MetaData(bind=migrate_engine)
pci_devices = Table("pci_devices", meta, autoload=True)
compute_nodes = Table("compute_nodes", meta, autoload=True)
fkey = ForeignKeyConstraint(columns=[pci_devices.c.compute_node_id], refcolumns=[compute_nodes.c.id])
fkey.create()
示例6: upgrade
def upgrade(migrate_engine):
meta = MetaData()
meta.bind = migrate_engine
jobs = Table('jobs', meta, autoload=True)
job_metadata = Table('job_metadata', meta, autoload=True)
if not job_metadata.foreign_keys:
cons = ForeignKeyConstraint([job_metadata.c.job_id], [jobs.c.id])
cons.create()
示例7: drop_foreign_key_constraints
def drop_foreign_key_constraints(constraint_names, columns, ref_columns):
"""Drop the foreign key constraints that match the given
criteria.
:param constraint_names: List of foreign key constraint names
:param columns: List of the foreign key columns.
:param ref_columns: List of the referenced columns.
"""
for constraint_name in constraint_names:
fkey_constraint = ForeignKeyConstraint(columns=columns, refcolumns=ref_columns, name=constraint_name)
fkey_constraint.drop()
示例8: upgrade
def upgrade(migrate_engine):
class AuthUserLog(Base):
"""
event:
L - Login
R - Register
P - Password
F - Forgot
"""
__tablename__ = 'auth_user_log'
__table_args__ = {"sqlite_autoincrement": True}
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey("auth_users.id", onupdate='CASCADE', ondelete='CASCADE'), index=True)
time = Column(DateTime(), default=func.now())
ip_addr = Column(Unicode(39), nullable=False)
internal_user = Column(Boolean, nullable=False, default=False)
external_user = Column(Boolean, nullable=False, default=False)
event = Column(Enum(u'L',u'R',u'P',u'F', name=u"event"), default=u'L')
recreate_constraints = [AuthUserLog]
# Upgrade operations go here. Don't create your own engine; bind
# migrate_engine to your metadata
debug = True
session.configure(bind=migrate_engine)
migrate_engine.echo=debug
metadata.bind = migrate_engine
metadata.reflect(only=['auth_users'])
r_meta = s.MetaData(migrate_engine, True)
def commit():
session.commit()
r_meta.bind.execute ('COMMIT;')
metadata.bind.execute('COMMIT;')
# create constraints
fks = []
for md in recreate_constraints:
t = md.__table__
rt = r_meta.tables[t.name]
rt_constraints = [a for a in rt.foreign_keys]
for cs in deepcopy(t.foreign_keys):
if cs.__class__.__name__ == 'ForeignKey':
table, column = cs.target_fullname.split('.')
target = [r_meta.tables[table].c[column]]
parent = [r_meta.tables[cs.parent.table.name].c[cs.parent.name]]
fk = ForeignKeyConstraint(columns=parent,refcolumns=target)
fk.use_alter = cs.use_alter
fk.ondelete = 'CASCADE'
fk.onupdate = 'CASCADE'
fk.name = cs.name
fks.append(fk)
if (cs.name in [a.name for a in rt_constraints]
or (cs.target_fullname
in [a.target_fullname for a in rt_constraints])):
fk.drop(migrate_engine)
commit()
for fk in fks:
fk.create(migrate_engine)
commit()
示例9: upgrade
def upgrade(migrate_engine):
metadata = sa.MetaData()
metadata.bind = migrate_engine
table_names = set(TABLES_FKEYS_SET_NULL.keys())
table_names.update(TABLES_COLUMNS_NOT_NULL.keys())
tables = {}
for t in table_names:
tables[t] = sautils.Table(t, metadata, autoload=True)
fks_to_change = []
# We need to parse the reflected model in order to find the automatic
# fk name that was put.
# Mysql and postgres have different naming convention so this is not very
# easy to have generic code working.
for t, keys in TABLES_FKEYS_SET_NULL.items():
table = tables[t]
for fk in table.constraints:
if not isinstance(fk, sa.ForeignKeyConstraint):
continue
for c in fk.elements:
if str(c.column) in keys:
# migrate.xx.ForeignKeyConstraint is changing the model
# so initializing here would break the iteration
# (Set changed size during iteration)
fks_to_change.append((
table, (fk.columns, [c.column]),
dict(name=fk.name, ondelete='SET NULL')))
for table, args, kwargs in fks_to_change:
fk = ForeignKeyConstraint(*args, **kwargs)
table.append_constraint(fk)
try:
fk.drop()
except NotSupportedError:
# some versions of sqlite do not support drop,
# but will still update the fk
pass
fk.create()
for t, cols in TABLES_COLUMNS_NOT_NULL.items():
table = tables[t]
if table.dialect_options.get('mysql', {}).get('engine') == 'InnoDB':
migrate_engine.execute('SET FOREIGN_KEY_CHECKS = 0;')
try:
for c in table.columns:
if c.name in cols:
c.alter(nullable=False)
finally:
if table.dialect_options.get('mysql', {}).get('engine') == 'InnoDB':
migrate_engine.execute('SET FOREIGN_KEY_CHECKS = 1;')
示例10: downgrade
def downgrade(migrate_engine):
# Operations to reverse the above upgrade go here.
meta = MetaData(bind=migrate_engine)
tt = Table('meas_IsotopeTable', meta, autoload=True)
t = Table('proc_FitTable', meta, autoload=True)
cons = ForeignKeyConstraint([t.c.isotope_id], [tt.c.id])
cons.drop()
try:
t.c.isotope_id.drop()
except:
pass
示例11: upgrade
def upgrade(migrate_engine):
# Upgrade operations go here. Don't create your own engine; bind
# migrate_engine to your metadata
meta = MetaData(bind=migrate_engine)
t = Table('proc_FitTable', meta, autoload=True)
try:
t.c.isotope.drop()
except:
pass
c = Column('isotope_id', Integer)
try:
c.create(t)
except:
pass
tt = Table('meas_IsotopeTable', meta, autoload=True)
cons = ForeignKeyConstraint([t.c.isotope_id], [tt.c.id])
cons.create()
示例12: downgrade
def downgrade(migrate_engine):
# Operations to reverse the above upgrade go here.
meta.bind = migrate_engine
tt = Table('gen_ProjectTable', meta, autoload=True)
cons = ForeignKeyConstraint([t.c.project_id], [tt.c.id])
cons.drop()
tt2 = Table('meas_AnalysisTable', meta, autoload=True)
cons = ForeignKeyConstraint([t2.c.analysis_id], [tt2.c.id])
cons.drop()
cons = ForeignKeyConstraint([t2.c.figure_id], [tt2.c.id])
cons.drop()
t.drop()
t2.drop()
示例13: downgrade
def downgrade(migrate_engine):
# Operations to reverse the above upgrade go here.
meta = MetaData(bind=migrate_engine)
mt = Table('meas_MeasurementTable', meta, autoload=True)
et = Table('meas_ExtractionTable', meta, autoload=True)
st = Table('meas_ScriptTable', meta, autoload=True)
fk = ForeignKeyConstraint([mt.c.script_id], [st.c.id])
fk.drop()
fk = ForeignKeyConstraint([et.c.script_id], [st.c.id])
fk.drop()
st.drop()
c = Column('script_blob', BLOB)
c.create(mt)
c = Column('hash', String(32))
c.create(mt)
c = Column('script_name', String(80))
c.create(mt)
# #
c = Column('script_blob', BLOB)
c.create(et)
c = Column('hash', String(32))
c.create(et)
c = Column('script_name', BLOB)
c.create(et)
mt.c.script_id.drop()
et.c.script_id.drop()
开发者ID:softtrainee,项目名称:arlab,代码行数:30,代码来源:059_modify_remove_hash_from_meas_and_extraction_table_add_extractscript_and_measscript_table.py
示例14: upgrade
def upgrade(migrate_engine):
# Upgrade operations go here. Don't create your own engine; bind
# migrate_engine to your metadata
meta = MetaData(bind=migrate_engine)
mt = Table('meas_MeasurementTable', meta, autoload=True)
et = Table('meas_ExtractionTable', meta, autoload=True)
mt.c.script_blob.drop()
mt.c.hash.drop()
mt.c.script_name.drop()
et.c.script_blob.drop()
et.c.hash.drop()
et.c.script_name.drop()
c = Column('script_id', Integer)
c.create(mt)
c = Column('script_id', Integer)
c.create(et)
st = Table('meas_ScriptTable', meta,
Column('id', Integer, primary_key=True),
Column('name', String(80)),
Column('hash', String(32)),
Column('blob', BLOB)
)
st.create()
fk = ForeignKeyConstraint([mt.c.script_id], [st.c.id])
fk.create()
fk = ForeignKeyConstraint([et.c.script_id], [st.c.id])
fk.create()
开发者ID:softtrainee,项目名称:arlab,代码行数:32,代码来源:059_modify_remove_hash_from_meas_and_extraction_table_add_extractscript_and_measscript_table.py
示例15: upgrade
def upgrade(migrate_engine):
# Upgrade operations go here. Don't create your own engine; bind
# migrate_engine to your metadata
meta.bind = migrate_engine
t.create()
t2.create()
tt = Table('gen_ProjectTable', meta, autoload=True)
cons = ForeignKeyConstraint([t.c.project_id], [tt.c.id])
cons.create()
tt2 = Table('meas_AnalysisTable', meta, autoload=True)
cons = ForeignKeyConstraint([t2.c.analysis_id], [tt2.c.id])
cons.create()
cons = ForeignKeyConstraint([t2.c.figure_id], [tt2.c.id])
cons.create()