【问题标题】:django.db.utils.ProgrammingError: relation does not existdjango.db.utils.ProgrammingError:关系不存在
【发布时间】:2021-03-30 15:04:17
【问题描述】:

我使用 Postgres db 开发了一个部署在 DigitalOcean 的 Ubuntu 服务器上的 Django 应用程序。一切正常,没有任何问题,但今天添加新模型后,我收到此错误: relation "documents_app_document" does not exist 虽然我有这个模型,但我的一些模型继承自 Document 模型。但不知何故,它已从数据库中删除,现在我无法在迁移后将其添加回数据库。如何将该模型作为表再次添加到数据库中?

p.s:但是我打开了名为“0001_initial.py”的迁移文件,里面有migrations.CreateModel( name='Document'...

models.py:

class Document(models.Model):
    created_date = models.DateTimeField(default=timezone.now, blank=True, null=True)
    added_by = CurrentUserField()
    purpose = models.CharField(blank=True, max_length=300, null=True)


    def __str__(self):
        return str(self.added_by)

class MedicalDocument(Document):
    policy_number = models.CharField(max_length=20, blank=True, null=True)
    medical_institution = models.CharField(max_length=100, blank=True, null=True) 

迁移错误:

Operations to perform:
  Unapply all migrations: documents_app
Running migrations:
  Rendering model states... DONE
  Unapplying documents_app.0001_initial...Traceback (most recent call last):
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "documents_app_document" does not exist


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

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 234, in handle
    fake_initial=fake_initial,
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/migrations/executor.py", line 121, in migrate
    state = self._migrate_all_backwards(plan, full_plan, fake=fake)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/migrations/executor.py", line 196, in _migrate_all_backwards
    self.unapply_migration(states[migration], migration, fake=fake)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/migrations/executor.py", line 269, in unapply_migration
    state = migration.unapply(state, schema_editor)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/migrations/migration.py", line 175, in unapply
    operation.database_backwards(self.app_label, schema_editor, from_state, to_state)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 120, in database_backwards
    schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 487, in remove_field
    self.execute(sql)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 137, in execute
    cursor.execute(sql, params)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "documents_app_document" does not exist

【问题讨论】:

  • 创建新模型后,是否运行了makemigrationsmigrate
  • 这也将有助于实际添加模型的代码,甚至可能是迁移的代码。
  • @GlennDJ,是的,我确实做到了。在迁移期间会发生此错误。好的,我正在添加
  • 您似乎没有创建模型MedicalDocument 的迁移。如果没有包含此内容的迁移,则运行 makemigrations 应该会创建一个包含它的新迁移。

标签: django postgresql django-models django-migrations


【解决方案1】:

添加更改/添加新模型后,请始终确保运行 python manage.py makemigrationspython manage.py migrate。 如果由于任何原因(迁移树重新排列、数据库故障等)出现问题,您可以通过执行python manage.py migrate {app_name} {migration_index} 来反转到特定迁移。 因此,在您的情况下,我建议您尝试python manage.py migrate {app_name} zero,然后重新迁移回最新版本。 您可能还需要使用--fake

【讨论】:

  • 我已经进行了迁移。命令python manage.py migrate {app_name} zero 再次对我产生同样的错误。
  • @BobReynolds 您是否尝试过使用--fake,然后重新应用它?听起来数据库状态与 Django 迁移状态不匹配,使用--fake 肯定可以解决。
  • 请看我完整的错误报告,也许你会找到一些提示。关于fake 我现在就试试。那么,假迁移后我该怎么办?
  • 我确实看过,请尝试fake。总是对我有用,这几乎就是它的用途。伪装后,你应该简单地运行python manage.py migrate without fake,一切都会正常运行。
  • 成功了,伙计。但在我的网站(生产)上仍然显示relation "documents_app_document" does not exist
猜你喜欢
  • 1970-01-01
  • 2018-05-11
  • 2014-10-25
  • 2019-03-15
  • 2018-03-23
  • 2021-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多