Connection: via IPv4
Your IP : 216.73.216.118
In English
Categories: LinuxServer

[メモ]docker環境でnetbox v4.0.xからv4.1以降への更新が失敗する場合の対処法

概要

v4.0.xからv4.1.x以降(v4.2.x、v4.3.x含む)へのマイグレーションに失敗する。

netbox-1  | ⚙️ Applying database migrations
netbox-1  |  loaded config '/etc/netbox/config/configuration.py'
netbox-1  |  loaded config '/etc/netbox/config/extra.py'
netbox-1  |  loaded config '/etc/netbox/config/logging.py'
netbox-1  |  loaded config '/etc/netbox/config/plugins.py'
netbox-1  | Operations to perform:
netbox-1  |   Apply all migrations: account, auth, circuits, contenttypes, core, dcim, django_rq, extras, ipam, sessions, social_django, taggit, tenancy, users, virtualization, vpn, wireless
netbox-1  | Running migrations:
netbox-1  | Traceback (most recent call last):
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 105, in _execute
netbox-1  |     return self.cursor.execute(sql, params)
netbox-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/psycopg/cursor.py", line 97, in execute
netbox-1  |     raise ex.with_traceback(None)
netbox-1  | psycopg.errors.UndefinedTable: relation "extras_objectchange" does not exist
netbox-1  | LINE 1: ...ct_type_id", "extras_objectchange"."user_id" FROM "extras_ob...
netbox-1  |                                                              ^
netbox-1  | 
netbox-1  | The above exception was the direct cause of the following exception:
netbox-1  | 
netbox-1  | Traceback (most recent call last):
netbox-1  |   File "/opt/netbox/netbox/./manage.py", line 10, in <module>
netbox-1  |     execute_from_command_line(sys.argv)
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
netbox-1  |     utility.execute()
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
netbox-1  |     self.fetch_command(subcommand).run_from_argv(self.argv)
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/core/management/base.py", line 416, in run_from_argv
netbox-1  |     self.execute(*args, **cmd_options)
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/core/management/base.py", line 460, in execute
netbox-1  |     output = self.handle(*args, **options)
netbox-1  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/core/management/base.py", line 107, in wrapper
netbox-1  |     res = handle_func(*args, **kwargs)
netbox-1  |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/core/management/commands/migrate.py", line 353, in handle
netbox-1  |     post_migrate_state = executor.migrate(
netbox-1  |                          ^^^^^^^^^^^^^^^^^
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/migrations/executor.py", line 135, in migrate
netbox-1  |     state = self._migrate_all_forwards(
netbox-1  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
netbox-1  |     state = self.apply_migration(
netbox-1  |             ^^^^^^^^^^^^^^^^^^^^^
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/migrations/executor.py", line 255, in apply_migration
netbox-1  |     state = migration.apply(state, schema_editor)
netbox-1  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/migrations/migration.py", line 132, in apply
netbox-1  |     operation.database_forwards(
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/migrations/operations/special.py", line 196, in database_forwards
netbox-1  |     self.code(from_state.apps, schema_editor)
netbox-1  |   File "/opt/netbox/netbox/extras/migrations/0117_move_objectchange.py", line 9, in update_content_types
netbox-1  |     ContentType.objects.using(db_alias).filter(app_label='core', model='objectchange').delete()
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/models/query.py", line 1196, in delete
netbox-1  |     collector.collect(del_query)
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/models/deletion.py", line 343, in collect
netbox-1  |     if getattr(on_delete, "lazy_sub_objs", False) or sub_objs:
netbox-1  |                                                      ^^^^^^^^
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/models/query.py", line 398, in __bool__
netbox-1  |     self._fetch_all()
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/models/query.py", line 1945, in _fetch_all
netbox-1  |     self._result_cache = list(self._iterable_class(self))
netbox-1  |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/models/query.py", line 91, in __iter__
netbox-1  |     results = compiler.execute_sql(
netbox-1  |               ^^^^^^^^^^^^^^^^^^^^^
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/models/sql/compiler.py", line 1623, in execute_sql
netbox-1  |     cursor.execute(sql, params)
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 79, in execute
netbox-1  |     return self._execute_with_wrappers(
netbox-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers
netbox-1  |     return executor(sql, params, many, context)
netbox-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 100, in _execute
netbox-1  |     with self.db.wrap_database_errors:
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/utils.py", line 91, in __exit__
netbox-1  |     raise dj_exc_value.with_traceback(traceback) from exc_value
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 105, in _execute
netbox-1  |     return self.cursor.execute(sql, params)
netbox-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1  |   File "/opt/netbox/venv/lib/python3.12/site-packages/psycopg/cursor.py", line 97, in execute
netbox-1  |     raise ex.with_traceback(None)
netbox-1  | django.db.utils.ProgrammingError: relation "extras_objectchange" does not exist
netbox-1  | LINE 1: ...ct_type_id", "extras_objectchange"."user_id" FROM "extras_ob...
netbox-1  |

解決策

# netboxのcompose.ymlがあるディレクトリに移動
cd /opt/netbox
# netbox関連を全部止める
docker compose down
# (Valkeyへの切り換えに関連して)redisのボリュームを2つ消す
docker volume list | grep redis
docker volume rm xxxx_netbox-redis-cache-data xxxx_netbox-redis-data
# postgresだけ起動
docker compose up postgres -d
# postgresがエラーを吐いてないか確認(エラーがあれば対処する)
docker compose ps
docker compose logs postgres
# postgresのpsqlを実行
docker compose exec postgres sh -c 'psql -U $POSTGRES_USER $POSTGRES_DB'
# マイグレーションの削除と権限の割り当て
DELETE from django_migrations WHERE name='0011_move_objectchange';
GRANT ALL ON SCHEMA public TO netbox;
# psqlから抜ける
\q
# フォアグランドで実行してマイグレーションが完了することを確認する
docker compose up

 

https://github.com/netbox-community/netbox/issues/17401#issuecomment-2344450383

 

メモ

弊運用環境ではredisからvalkeyへの切り換えによるrdb読み込みエラー、postgresのバージョンアップ、netboxのマイグレーション失敗が複合していたので非常に厄介だったが当該issueを見つけた時点で前者2つの問題は解決済みだったため、実際はマイグレーションの削除と権限のsqlだけ実行した。

This post was last modified on 2025年5月27日 8:45 AM

This website uses cookies.