使用Alembic升级脚本更改MySQL列字符集
我想把一个列的字符集从 utf8mb4_unicode_ci 改成 utf8mb4_bin。
这是我更新后的 SqlAlchemy 模型:
col_name = Column(VARCHAR(10, collation='utf8mb4_bin'), nullable=True)
我尝试过
from alembic import op
import sqlalchemy as sa
import sqlalchemy.types as ty
def upgrade():
op.alter_column('table_name',
sa.Column('col_name', ty.VARCHAR(10, collation='utf8mb4_bin') )
)
但是升级后,这个列在 MySQL 中的字符集还是保持原来的 utf8mb4_unicode_ci。
1 个回答
2
找到了正确的语法,也就是:
from alembic import op
import sqlalchemy.types as ty
def upgrade():
op.alter_column('table_name','col_name', type_=ty.VARCHAR(10, collation='utf8mb4_bin'))