SqlAlchemy changes in Model description not applied in DB

泪湿孤枕 提交于 2019-12-23 03:07:35

问题


I am currently developing a server using Flask/SqlAlchemy. It occurs that when an ORM model is not present as a table in the database, it is created by default by SqlAlchemy.

However when an ORM class is changed with for instance an extra column is added, these changes do not get saved in the database. So the extra column will be missing, every time I query. I have to adjust my DB manually every time there is a change in the models that I use.

Is there a better way to apply changes in the models during development? I hardly think manual MySql manipulation is the best solution.


回答1:


you can proceed as the following:

new_column = Column('new_column', String, default='some_default_value')
new_column.create(my_table, populate_default=True)

you can find more details about sqlalchemy migration in: https://sqlalchemy-migrate.readthedocs.org/en/latest/changeset.html



来源:https://stackoverflow.com/questions/28832827/sqlalchemy-changes-in-model-description-not-applied-in-db

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