django.db.utils.ProgrammingError: 关系已经存在于 OenBSD vps

Posted

技术标签:

【中文标题】django.db.utils.ProgrammingError: 关系已经存在于 OenBSD vps【英文标题】:django.db.utils.ProgrammingError: relation already exists on OenBSD vps 【发布时间】:2017-02-09 18:59:42 【问题描述】:

我收到此错误。我尝试使用 migrate --fake default ,但它似乎不起作用。附件是“python manage.py migrate”的输出 我的设置是 OneBSD VPS 上的 Django 1.6 + celery3.1.12 + postgresql + gunicorn。

Running migrations for users:


- Migrating forwards to 0007_auto__del_field_profile_weekly_digest__del_field_profile_daily_digest_.
 > users:0001_initial
FATAL ERROR - The following SQL query failed: CREATE TABLE "users_user" ("id" serial NOT NULL PRIMARY KEY, "password" varchar(128) NOT NULL, "last_login" timestamp with time zone NOT NULL, "email" varchar(255) NOT NULL UNIQUE, "name" varchar(255) NOT NULL, "is_active" boolean NOT NULL, "is_admin" boolean NOT NULL, "is_staff" boolean NOT NULL, "type" integer NOT NULL, "status" integer NOT NULL, "new_messages" integer NOT NULL, "badges" integer NOT NULL, "score" integer NOT NULL, "full_score" integer NOT NULL, "flair" varchar(15) NOT NULL, "site_id" integer NULL)
The error was: relation "users_user" already exists

Error in migration: users:0001_initial
Traceback (most recent call last):
  File "manage.py", line 9, in <module>
    execute_from_command_line(sys.argv)
  File "/home/jay/biostar-central/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/home/jay/biostar-central/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/jay/biostar-central/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/jay/biostar-central/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/home/jay/biostar-central/lib/python2.7/site-packages/south/management/commands/migrate.py", line 111, in handle
    ignore_ghosts = ignore_ghosts,
  File "/home/jay/biostar-central/lib/python2.7/site-packages/south/migration/__init__.py", line 220, in migrate_app
    success = migrator.migrate_many(target, workplan, database)
  File "/home/jay/biostar-central/lib/python2.7/site-packages/south/migration/migrators.py", line 254, in migrate_many
    result = migrator.__class__.migrate_many(migrator, target, migrations, database)
  File "/home/jay/biostar-central/lib/python2.7/site-packages/south/migration/migrators.py", line 329, in migrate_many
    result = self.migrate(migration, database)
  File "/home/jay/biostar-central/lib/python2.7/site-packages/south/migration/migrators.py", line 133, in migrate
    result = self.run(migration, database)
  File "/home/jay/biostar-central/lib/python2.7/site-packages/south/migration/migrators.py", line 114, in run
    return self.run_migration(migration, database)
  File "/home/jay/biostar-central/lib/python2.7/site-packages/south/migration/migrators.py", line 84, in run_migration
    migration_function()
  File "/home/jay/biostar-central/lib/python2.7/site-packages/south/migration/migrators.py", line 60, in <lambda>
    return (lambda: direction(orm))
  File "/home/jay/biostar-central/biostar/apps/users/migrations/0001_initial.py", line 28, in forwards
    ('site', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['sites.Site'], null=True)),
  File "/home/jay/biostar-central/lib/python2.7/site-packages/south/db/generic.py", line 47, in _cache_clear
    return func(self, table, *args, **opts)
  File "/home/jay/biostar-central/lib/python2.7/site-packages/south/db/generic.py", line 361, in create_table
    "columns": ', '.join([col for col in columns if col]),
  File "/home/jay/biostar-central/lib/python2.7/site-packages/south/db/generic.py", line 282, in execute
    cursor.execute(sql, params)
  File "/home/jay/biostar-central/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/home/jay/biostar-central/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/jay/biostar-central/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "users_user" already exists

【问题讨论】:

【参考方案1】:

如果python manage.py migrate --fake-initial 不起作用,您可以尝试从您的应用中删除迁移文件夹并通过以下方式创建新文件夹:

python manage.py makemigrations app_name

【讨论】:

非常感谢。【参考方案2】:

这对我有用:

python manage.py migrate --fake

【讨论】:

db没有变化,新创建的表没有添加【参考方案3】:

我知道已经很晚了,但是我在 Django 1.11 中遇到了这个问题。我使用了一个 table-django 模型,它需要另一个依赖于以前模型的 TableModel。我不得不从我的初始迁移中删除这些。

所以检查你的迁移文件

app/migrations/some_migration.py

可能有一个表被引用了两次。

【讨论】:

以上是关于django.db.utils.ProgrammingError: 关系已经存在于 OenBSD vps的主要内容,如果未能解决你的问题,请参考以下文章