Search code examples
djangodjango-migrations

AttributeError: 'str' object has no attribute 'state_forwards' with django migrations migrations.RunSQL(


I am trying to set the autogenerated id of my postgres tables to a start value of 10000 for different models. I used this article and did the following:

python3 manage.py makemigrations core --empty

Then in the migration I added:

    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;"
                        ),
    ]

I put these in two separate lines because I was getting an error that __init.py__ could only take 6 commands btw.

When I run migrate on this, I get:

  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'

I also tried to run these commands individually in my psql shell and they work so I do not think it is an issue with the models or tables. I want to put this in my migrations though so I make sure it happens if another person sets up a local db. What is state_forwards any why do you get this error?

Thanks for your help!


Solution

  • only the first of RunSql parameters is a query.

    the signature is :RunSQL(sql, reverse_sql=None, state_operations=None, hints=None, elidable=False)

    so you passed your 3rd query as state_operation which expects a list of operations instead.

    you need to have 1 RunSql per query or, according to the documentation, pass an array of sql queries :

    operations = [
      migrations.RunSQL([
        "QUERY1",
        "QUERY2",
        ...
        ])
    ]