[英]Add a column to existing class of sqlalchemy
我有一個生成唯一 id 的父 class 和一個具有用戶名的子 class。
class Ids(object):
id = Column(Integer, Sequence('res_id_seq'), primary_key=True)
class Names(Ids, base):
username = Column(String(50))
# I have created an sqlite db and persisting using sqlalchemy
engine = create_engine('sqlite:///emp.db', echo=True)
base.metadata.create_all(engine)
我想在名稱(姓氏)中添加一列。 我怎么能用 sqlalchemy-migrate 做到這一點? 有什么例子會有所幫助嗎?
根據sqlalchemy-migrate 文檔,使用可以在遷移中的列上使用create
和drop
方法。 您的遷移腳本可能會像這樣循環:
from sqlalchemy import Column, Integer, MetaData, String, Table
metadata = MetaData()
# Note that here you use Table instance
# not the class derived from sqlalchemy.ext.declarative.declarative_base
names = Table(
'names',
metadata,
# You need to specify only primary key column
# if you just adding a new column in this script
Column('id', Integer, primary_key=True),
)
lastname = Column('lastname', String(50))
def upgrade(migrate_engine):
metadata.bind = migrate_engine
lastname.create(names)
def downgrade(migrate_engine):
metadata.bind = migrate_engine
names.append_column(lastname)
names.c.lastname.drop()
更多關於創建遷移腳本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.