我有一个表,我需要在a列之后添加a_1列,表结构类似于a,b,c,d列?我怎么才能添加这个?
下面的查询在d之后添加列,但我想在a?之后添加列。
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('sample_table', sa.Column('a_1', mysql.JSON(), nullable=True, ))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('sample_table', 'a_1')
# ### end Alembic commands ###发布于 2021-03-19 20:16:49
add_column(table_name, column, schema=None)使用当前迁移上下文发出“add column”指令。
例如:
from alembic import op
from sqlalchemy import Column, String
op.add_column('organization',
Column('name', String())
)提供的列对象还可以指定引用远程表名称的ForeignKey。Alembic将自动生成一个存根“引用”表,并发出第二个ALTER语句,以便单独添加约束:
from alembic import op
from sqlalchemy import Column, INTEGER, ForeignKey
op.add_column('organization',
Column('account_id', INTEGER, ForeignKey('accounts.id'))
)请注意,此语句使用SQLAlchemy库中的列结构。特别是,要在数据库端创建的默认值是使用server_default参数指定的,而不是仅指定Python端默认值的default:
from alembic import op
from sqlalchemy import Column, TIMESTAMP, func
# specify "DEFAULT NOW" along with the column add
op.add_column('account',
Column('timestamp', TIMESTAMP, server_default=func.now())
)Parameters table_name -父表的字符串名称。
column -表示新列的sqlalchemy.schema.Column对象。
schema -要在其中进行操作的可选架构名称。要在缺省行为之外控制模式的引用,请使用SQLAlchemy构造quoted_name。
https://stackoverflow.com/questions/66704308
复制相似问题