首页 > 解决方案 > django-sorcery 不会自动生成迁移

问题描述

我正在玩django-sorcery库,并且一直在尝试生成一些迁移:

django-admin sorcery revision -m "Second revision" --autogenerate -v 1 my_app

当我第一次运行命令时,我看到一条错误消息:

FileNotFoundError: [Errno 2] No such file or directory: [... snip ...]python3.7/site-packages/django_sorcery/db/alembic/script.py.mako

不知道为什么 pip 包中缺少模板,我手动将其添加回来。

现在,该命令只生成空的迁移文件,即使我指定了 --autogenerate 标志。

我应该在某处扔一个 env.py 文件吗?这个命令实际上应该工作吗?帮助表示赞赏。

标签: pythondjangosqlalchemydjango-sorcery

解决方案


缺少的模板实际上已在最近的版本中得到解决,因此建议您尝试一下。

至于没有被选中的模型,一些可能有用的想法

  • 模型需要是一个魔法模型。换句话说,它需要子类化db.Model
from django_sorcery.db import databases
db = databases.get("default")
class MyModel(db.Model):
    ...
  • 模型需要在其中之一中定义INSTALLED_APPS。内部巫术用于get_containing_app_config获取模型的相应 django 应用程序,因为 sqlalchemy 模型未在 django 应用程序中本地注册

  • 默认情况下不需要其他任何东西,因此不需要传统的env.py. 应用程序中允许进行一些基本的自定义AppConfig

    • version_table- 默认为alembic_version_{app.label}
    • version_table_schema

    如果还不够,有几个信号可用于连接到 alembic 配置创建以进行自定义

这应该使迁移命令起作用。repo 附带一个用于测试test_site的规范应用程序。polls您可以尝试使用它和 alembic 迁移:

$ cd test_site
$ ./manage.py sorcery revision -m 'initial migration' -r 0001 --autogenerate polls
$ cat polls/migrations/0001_initial_migration.py
...
def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('question',
    sa.Column('pk', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('question_text', sa.String(length=200), nullable=True),
    sa.Column('pub_date', sa.DateTime(), nullable=True),
    sa.PrimaryKeyConstraint('pk')
    )
    op.create_table('choice',
    sa.Column('pk', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('choice_text', sa.String(length=200), nullable=True),
    sa.Column('votes', sa.Integer(), nullable=True),
    sa.Column('question_pk', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['question_pk'], ['question.pk'], ),
    sa.PrimaryKeyConstraint('pk')
    )
    # ### end Alembic commands ###
...

免责声明:我是 django-sorcery 的维护者之一。感谢您的尝试!如果您遇到任何错误,请随时打开问题。很多东西还没有很好的记录,因为我们正在构建功能,但希望随着时间的推移文档和稳定性会得到改善。


推荐阅读