首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AttributeError:'str‘对象没有带有django迁移migrations.RunSQL的属性'state_forwards’(

AttributeError:'str‘对象没有带有django迁移migrations.RunSQL的属性'state_forwards’(
EN

Stack Overflow用户
提问于 2020-09-03 04:49:20
回答 1查看 781关注 0票数 0

我正在尝试将postgres表自动生成的id设置为不同型号的起始值10000。我使用了this article并执行了以下操作:

python3 manage.py makemigrations core --empty

然后在迁移中我添加了:

代码语言:javascript
运行
复制
    operations = [
        migrations.RunSQL(
                        "ALTER SEQUENCE CORE_ORG_id_seq RESTART WITH 10000",
                        "ALTER SEQUENCE CORE_USER_id_seq RESTART WITH 10000",
                        "ALTER SEQUENCE CORE_PARTLISTING_id_seq RESTART WITH 1000",
                        "ALTER SEQUENCE CORE_BOMITEM_id_seq RESTART WITH 1000",
                        "ALTER SEQUENCE CORE_MANUFACTURER_id_seq RESTART WITH 1000;"
        ),
        migrations.RunSQL(
                        "ALTER SEQUENCE CORE_BOM_id_seq RESTART WITH 1000",
                        "ALTER SEQUENCE CORE_SPECIFICPART_id_seq RESTART WITH 10000",
                        "ALTER SEQUENCE CORE_GENERICPART_id_seq RESTART WITH 10000;"
                        ),
    ]

我将它们放在两个单独的行中,因为我得到了一个错误,__init.py__只能接受6个命令。

当我在上面运行migrate时,我得到:

代码语言:javascript
运行
复制
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/Users/myname/.local/share/virtualenvs/mobiusAPI-NVQT3lgx/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/Users/myname/.local/share/virtualenvs/mobiusAPI-NVQT3lgx/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/myname/.local/share/virtualenvs/mobiusAPI-NVQT3lgx/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/myname/.local/share/virtualenvs/mobiusAPI-NVQT3lgx/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/Users/myname/.local/share/virtualenvs/mobiusAPI-NVQT3lgx/lib/python3.8/site-packages/django/core/management/base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Users/myname/.local/share/virtualenvs/mobiusAPI-NVQT3lgx/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 243, in handle
    post_migrate_state = executor.migrate(
  File "/Users/myname/.local/share/virtualenvs/mobiusAPI-NVQT3lgx/lib/python3.8/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/Users/myname/.local/share/virtualenvs/mobiusAPI-NVQT3lgx/lib/python3.8/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/Users/myname/.local/share/virtualenvs/mobiusAPI-NVQT3lgx/lib/python3.8/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/myname/.local/share/virtualenvs/mobiusAPI-NVQT3lgx/lib/python3.8/site-packages/django/db/migrations/migration.py", line 114, in apply
    operation.state_forwards(self.app_label, project_state)
  File "/Users/myname/.local/share/virtualenvs/mobiusAPI-NVQT3lgx/lib/python3.8/site-packages/django/db/migrations/operations/special.py", line 101, in state_forwards
    state_operation.state_forwards(app_label, state)
AttributeError: 'str' object has no attribute 'state_forwards'

我还尝试在psql shell中单独运行这些命令,它们可以正常工作,所以我不认为这是模型或表的问题。我想把它放在我的迁移中,这样我就可以确保如果另一个人建立了一个本地数据库,就会发生这种情况。什么是state_forwards any,为什么会出现这个错误?

谢谢你的帮忙!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-03 04:54:25

只有第一个RunSql参数是查询。

签名是:RunSQL(sql, reverse_sql=None, state_operations=None, hints=None, elidable=False)

因此,您将第三个查询作为state_operation传递,该查询需要一个操作列表。

您需要为每个查询提供一个RunSql,或者根据文档,传递一组sql查询:

代码语言:javascript
运行
复制
operations = [
  migrations.RunSQL([
    "QUERY1",
    "QUERY2",
    ...
    ])
]
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63713347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档