python - Django 是否试图两次制作同一张 table ?

标签 python django postgresql

我对 Django 有一个非常难以理解的问题。

我使用 models.py 为我的数据库建模。该数据库在生产服务器上运行。我想改变它,所以我在开发服务器上创建了一个新的数据库。问题发生在我执行 syncdb 时,Django 尝试创建两次相同的表,如下所示:

python manage.py syncdb

Operations to perform:
  Synchronize unmigrated apps: inventaryApp, newPrpvApp, grappelli, debug_toolbar, registration, import_export
  Apply all migrations: sitetree, sessions, admin, sites, auth, contenttypes
Synchronizing apps without migrations:
  Creating tables...
    Creating table u_institutions
    Creating table u_users
    Creating table o_organes
    Creating table o_degats
    Creating table o_validation
    Creating table o_m_determination
    Creating table o_stades_dvlpmt
    Creating table o_types
    Creating table precision_date
    Creating table t_categories
    Creating table t_etat_synonymie
    Creating table g_langues
    Creating table g_pays
    Creating table g_regions
    Creating table g_communes
    Creating table pays_langues
    Creating table newprpvapp_userprofile
    Creating table t_noms_verna
    Creating table langues_noms_verna
    Creating table maladies
    Creating table t_familles
    Creating table t_genres
    Creating table t_especes
    Creating table images
    Creating table esp_noms_verna
    Creating table t_infra_types
    Creating table t_infra_sp
    Creating table t_synonymes
    Creating table o_contexte
    Creating table o_interception
    Creating table o_hotes
    Creating table hotes_organes
    Creating table o_organismes_associes
    Creating table u_institutions

如您所见,u_institutions 表是最先创建的,也是最后创建的。这是命令的执行停止显示的地方:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/base.py", line 533, in handle
    return self.handle_noargs(**options)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 27, in handle_noargs
    call_command("migrate", **options)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 115, in call_command
    return klass.execute(*args, **defaults)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 128, in handle
    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 239, in sync_apps
    cursor.execute(statement)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 63, in execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "u_institutions" already exists

我已验证该表没有在 model.py 中设置两次,事实并非如此。我使用 Django 1.7psycopg 2.6。请问你有什么想法吗?谢谢大家!

编辑

我已经执行了 makemigrations,一切都很好。现在它表示没有检测到任何变化,这可能是正常的。

当我尝试 migrate 时,我得到:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 128, in handle
    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 239, in sync_apps
    cursor.execute(statement)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 63, in execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "u_institutions" already exists

migrate --fake 返回:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 128, in handle
    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 239, in sync_apps
    cursor.execute(statement)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 63, in execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "u_institutions" already exists

最佳答案

您似乎已经创建了数据库架构。请使用迁移(只要您使用 Django 1.7)。 syncdb 已弃用。

python manage.py makemigrations
python manage.py migrate

如果您在此处遇到任何错误(例如 relation "..."already exists),请运行此命令将此表标记为已应用:

python manage.py migrate --fake

关于python - Django 是否试图两次制作同一张 table ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30045309/

相关文章:

python - PyQt : How to create a lineItem between two button and can move with button?

python - 使用 Django REST 框架从多个模型返回结果

Postgresql -> 来自简单更新的死锁。我找不到原因

python - 在 python 中使用 openpyxl 模块编写 xlsx 文件

python - 检测 x y 图中尖峰的算法

python - 运行更多任务的 Celery 任务

Django - 在 "select"中添加在线列

python - 在 django 1.7.6 上运行 django-oscar 的 Heroku 上从 Python 2.7 切换到 Python 3.4 时出现导入错误

postgresql - 按 DOW 划分的销售计数(以日期和时间作为输入)- postgresql

python - 无法安装 psycopg2 Ubuntu