postgresql django.db.utils.ProgrammingError:关系不存在

epggiuax  于 2023-11-18  发布在  PostgreSQL
关注(0)|答案(3)|浏览(160)

我开发了一个Django应用程序,部署在DigitalOcean的Ubuntu服务器上,使用Postgres db。一切正常,没有任何问题,但今天添加新模型后,我得到了这个错误: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

isr3a4wc

isr3a4wc1#

在添加/更改/添加新模型之后,始终确保运行python manage.py makemigrationspython manage.py migrate。(迁移树重新排列,数据库失败等)出了问题,你可以通过执行python manage.py migrate {app_name} {migration_index}来返回到特定的迁移。所以我建议你尝试python manage.py migrate {app_name} zero,然后重新迁移回最新版本。您可能还需要使用--fake

mgdq6dx1

mgdq6dx12#

以下是一个可能的解决方法:删除旧的迁移。注解掉所有模型中与Document模型相关的所有字段,并执行makemigrationsmigrate以单独创建“文档”表。取消注解其他模型中与Document相关的字段,然后重新进行迁移。

rdrgkggo

rdrgkggo3#

我遇到了(看起来)同样的问题。无法运行迁移或任何东西.
我的理由是:
我有2个数据库(带DB路由器)。在一个 db2 模型中,我使用一个函数来生成选项,该函数使用数据库1的模型X中的数据。我在创建数据库1.模型X后编写了该函数。
当我现在尝试在没有新的Database-1.Model-X的生产系统上部署新的迁移时,我得到了相同的错误。
我看到了三种可能性:

  • 在应用迁移之前注解掉访问不存在的模型的函数(有点蹩脚而且容易出错)
  • 将选择生成从模型移到表单(没有改变任何东西)
  • 更改/扩展数据库路由器以允许数据库之间的外键关系,使用与数据库1的外键关系,同时选择外键定义中所需的(唯一)字段to_field=“XYZ”,以便 db2 字段的内容填充正确的数据。

也许这对有同样或类似问题的人来说是一个启发。
问候
卡尔

相关问题