Trouble when using alembic with sqlalchemy_utils

时间秒杀一切 提交于 2019-12-22 08:11:09

问题


In my sqlalchemy model i use sqlalchemy_utils' choicetype:

id = db.Column(db.Integer, primary_key=True)
code = db.Column(db.Integer, nullable=True)
level = db.Column(mytypes.types.ChoiceType(LEVEL))

I did everything as described here http://alembic.readthedocs.org/en/latest/autogenerate.html#autogen-module-prefix. In my model i imported choicetype from my module mytypes.types:

from sqlalchemy_utils.types.choice import ChoiceType

, in alembic/env.py i added context

context.configure(
    connection=connection,
    target_metadata=target_metadata,
    user_module_prefix="mytypes.types."
    # ...
)

, and in the script.py.mako

import mytypes.types

.The problem is when i am making revision of my model, i getting something
like this

from alembic import op
import sqlalchemy as sa
import mytypes.types

def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.add_column('logging', sa.Column('level', mytypes.types.ChoiceType(length=255), nullable=True))
### end Alembic commands ###

Why alembic didn't pass "LEVEL" argument to choicetype and why it passed length=255 instead?


回答1:


I fixed it by manually change this mytypes.types.ChoiceType(length=255) to mytypes.types.ChoiceType(MyEnum) and import it.




回答2:


Alembic and SqlAlchemy-utils are not friends like we expect them to be. So Do import your models file in into your alembic migration version file and edit the upgrade function accordingly.

Like it's done here https://sqlalchemy-utils.readthedocs.io/en/latest/data_types.html#module-sqlalchemy_utils.types.choice



来源:https://stackoverflow.com/questions/30132370/trouble-when-using-alembic-with-sqlalchemy-utils

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!