FounderofScrhyme
@FounderofScrhyme

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

djangoでmakemigrations成功後のmigrate実行時に起こるdjango.db.utils.InternalErrorを解決したいです

djangoでmakemigrations成功後のmigrate実行時に起こるdjango.db.utils.InternalErrorを解決したいです.

動かして学ぶpython django開発入門という書籍のサンプルアプリ作成でp246のdjango-allauth用のマイグレーションを行った際

django.db.utils.InternalError: cannot drop sequence account_emailaddress_id_seq because column id of table account_emailaddress requires it
HINT: You can drop column id of table account_emailaddress instead.

というエラーが起こり解決できないまま二日経ちました。

以下がメッセージ全文になります。

Operations to perform:
Apply all migrations: account, accounts, admin, auth, contenttypes, sessions, sites
Running migrations:
Applying account.0003_auto_20231020_1301...Traceback (most recent call last):
File "/Users/****/virtual_private_diary/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.DependentObjectsStillExist: cannot drop sequence account_emailaddress_id_seq because column id of table account_emailaddress requires it
HINT: You can drop column id of table account_emailaddress instead.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/Users//virtual_private_diary/private_diary/manage.py", line 22, in
main()
File "/Users/
/virtual_private_diary/private_diary/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/Users//virtual_private_diary/lib/python3.8/site-packages/django/core/management/init.py", line 419, in execute_from_command_line
utility.execute()
File "/Users/
/virtual_private_diary/lib/python3.8/site-packages/django/core/management/init.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users//virtual_private_diary/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(args, **cmd_options)
File "/Users/
/virtual_private_diary/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(args, **options)
File "/Users/
/virtual_private_diary/lib/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
res = handle_func(args, **kwargs)
File "/Users/
/virtual_private_diary/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 244, in handle
post_migrate_state = executor.migrate(
File "/Users/
/virtual_private_diary/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/
/virtual_private_diary/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/
/virtual_private_diary/lib/python3.8/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/
/virtual_private_diary/lib/python3.8/site-packages/django/db/migrations/migration.py", line 126, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/
/virtual_private_diary/lib/python3.8/site-packages/django/db/migrations/operations/fields.py", line 244, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/Users/
/virtual_private_diary/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 608, in alter_field
self._alter_field(model, old_field, new_field, old_type, new_type,
File "/Users/
/virtual_private_diary/lib/python3.8/site-packages/django/db/backends/postgresql/schema.py", line 196, in _alter_field
super()._alter_field(
File "/Users/
/virtual_private_diary/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 794, in _alter_field
self.execute(sql, params)
File "/Users/
/virtual_private_diary/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 145, in execute
cursor.execute(sql, params)
File "/Users/
/virtual_private_diary/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/Users/
/virtual_private_diary/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Users/
/virtual_private_diary/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/
/virtual_private_diary/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Users/
/virtual_private_diary/lib/python3.8/site-packages/django/db/utils.py", line 90, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/
***/virtual_private_diary/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.InternalError: cannot drop sequence account_emailaddress_id_seq because column id of table account_emailaddress requires it
HINT: You can drop column id of table account_emailaddress instead.

プログラミング初心者で詳しいことは分かりませんがHINTに記載のあるaccount emailaddressといったテーブルを自身で定義していないため自動作成されたコードに問題がある気がします。
pycharmでdjangoを使ったウェブアプリ制作を行っており、バージョンはpython3.8.10、allauth0.45、django3.2.7、データベースはpostgresql、ドライバはpsycopg2-binary2.9.1です

試したこと

書籍のサンプルコードをダウンロードし貼り付けて実行したためタイポがないことは確認しています。
フルパスでも、makemigrationsで自動作成されたマイグレーションファイルを含むディレクトリでもmigrateを実行しました。

解決方法のわかる方がおられましたらご教示お願いいたします。

0

1Answer

私にはエラーログが大量でコードブロックされてないので読み取れませんでした。以下は一般的な説明です。

書籍のサンプルアプリ作成でp246

が手元にないので何が悪いか?不明ですがwsgiの仕組みの理解とdjangoのweb環境構築が完了しているなら、DBの定義とモデルフィールドの定義を行い、マイグレーションするだけです。

モデルフィールドとSQLの変換がイメージできる方と全くイメージできない方とではエラー箇所に辿り着くか?分かれ道なのかもしれません。

サンプルURLと書籍サンプルを比較してDBの定義とモデルフィールドの定義の差異を確認してはどうでしょう?

0Like

Your answer might help someone💌