问题描述
我目前正在使用Flask/SqlAlchemy开发服务器.当数据库中没有ORM模型作为表存在时,默认情况下会由SqlAlchemy创建它.
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.
但是,当用一个额外的列更改ORM类时,这些更改不会保存在数据库中.因此,每次查询时,多余的列都会丢失.每当使用的模型发生变化时,我都必须手动调整数据库.
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.
在开发过程中是否有更好的方法来应用模型中的更改?我几乎认为手动MySql操作是最好的解决方案.
Is there a better way to apply changes in the models during development? I hardly think manual MySql manipulation is the best solution.
推荐答案
您可以按以下步骤进行操作:
you can proceed as the following:
new_column = Column('new_column', String, default='some_default_value')
new_column.create(my_table, populate_default=True)
您可以在以下位置找到有关sqlalchemy迁移的更多详细信息: https://sqlalchemy- migration.readthedocs.org/en/latest/changeset.html
you can find more details about sqlalchemy migration in: https://sqlalchemy-migrate.readthedocs.org/en/latest/changeset.html
这篇关于模型说明中的SqlAlchemy更改未在DB中应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!