“ Django_content_type已经存在”-在数据库还原后在服务器上运行迁移之后

时间:2019-04-29 06:32:37

标签: django database migration psql contenttypes

我已经迁移到服务器环境,最后一切正常。 django项目可以正确呈现,但是迁移/数据库似乎存在很大问题。

当我尝试运行迁移时,我遇到了这个错误,它正在影响其他因素(例如我用来填充项目的脚本)

 File "/home//.virtualenvs/versal/lib/python3.7/site-packages/djang                                              o/db/backends/utils.py", line 83, in _execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "django_content_type" already exists

(versal) 06:18 ~/Versal/versal (master)$  Failing row contains (null, contenttypes, 0001_initial, 2019-04-29 06:11:02.087041+0

我试图清理迁移并再次运行它们,但这没有用,因为这是'showmigrations'所显示的。

accounts
 [ ] 0001_initial
admin
 [ ] 0001_initial
 [ ] 0002_logentry_remove_auto_add
 [ ] 0003_logentry_add_action_flag_choices
auth
 [ ] 0001_initial
 [ ] 0002_alter_permission_name_max_length
 [ ] 0003_alter_user_email_max_length
 [ ] 0004_alter_user_username_opts
 [ ] 0005_alter_user_last_login_null
 [ ] 0006_require_contenttypes_0002
 [ ] 0007_alter_validators_add_error_messages
 [ ] 0008_alter_user_username_max_length
 [ ] 0009_alter_user_last_name_max_length
blog
 [ ] 0001_initial
 [ ] 0002_auto_20190225_1221
 [ ] 0003_auto_20190225_1251
contenttypes
 [ ] 0001_initial
 [ ] 0002_remove_content_type_name
core
 [ ] 0001_initial
 [ ] 0002_auto_20190331_1730
 [ ] 0003_auto_20190404_0920
curate
 [ ] 0001_initial
 [ ] 0002_item_video
 [ ] 0003_set
 [ ] 0004_auto_20190424_1620
 [ ] 0005_item_tags
django_comments
 [ ] 0001_initial
 [ ] 0002_update_user_email_field_length
 [ ] 0003_add_submit_date_index
podcast
 [ ] 0001_initial
 [ ] 0002_remove_episode_order
 [ ] 0003_remove_show_ttl
 [ ] 0004_auto_20170917_0503
 [ ] 0005_episode_guid
 [ ] 0006_auto_20170920_1833
 [ ] 0007_auto_20170920_2212
 [ ] 0008_auto_20170920_2212
 [ ] 0009_auto_20170923_0129
 [ ] 0010_auto_20170923_0131
 [ ] 0011_episode_itunes_title
 [ ] 0012_episode_type
 [ ] 0013_episode_number
 [ ] 0014_episode_season
 [ ] 0015_show_type
 [ ] 0016_auto_20170923_2000
 [ ] 0017_auto_20170923_2133
 [ ] 0018_auto_20170924_0609
 [ ] 0019_auto_20170924_1839
 [ ] 0020_auto_20171026_2121
 [ ] 0021_auto_20171028_1742
 [ ] 0022_remove_category_json
 [ ] 0023_auto_20171121_2139
 [ ] 0024_show_ttl
 [ ] 0025_auto_20171211_0342
 [ ] 0026_auto_20171211_0342
 [ ] 0027_auto_20171211_0424
 [ ] 0028_auto_20171211_0424
 [ ] 0029_auto_20171211_0424
 [ ] 0030_auto_20190426_1051
sessions
 [ ] 0001_initial
sites
 [ ] 0001_initial
 [ ] 0002_alter_domain_unique
taggit
 [ ] 0001_initial
 [ ] 0002_auto_20150616_2121
 [ ] 0003_taggeditem_add_unique_index
threadedcomments
 [ ] 0001_initial
 [ ] 0002_auto_20150521_2011
 [ ] 0003_threadedcomment_newest_activity

我在本地计算机上拥有这些迁移-我可以通过git push将它们重新添加到它们所在的位置。但是问题仍然存在。

我的预期结果是,我可以每次进行迁移和迁移而不会出错...

如果在进行迁移后运行--fake-initial,我会得到:

django.db.utils.IntegrityError: null value in column "id" violates not-null constraint
DETAIL:  Failing row contains (null, contenttypes, 0001_initial, 2019-04-29 06:48:55.697347+00).

我还应该说,如果我运行命令makemigrations,我会得到“未检测到更改”,因此从这个意义上说,Django比较高兴。

2 个答案:

答案 0 :(得分:0)

我在新托管的网站中被类似问题阻止。这是我的情况。

  1. 我在我们的开发localhost中拥有MySQL数据库的工作站点。
  2. 我使用Postgres db在Digital Ocean的Ubuntu 18.04 Droplet中创建了新的托管服务器。
  3. 构建此托管服务器时,我用空数据库和空Django站点测试了服务器。
  4. 我使用pgAdmin工具手动将MySQL数据库迁移到Postgres,并在virtualenv中运行“ loaddata”命令将数据加载到Postgress。
  5. 现在,当我运行以下命令时,我看到的问题与此处报告的相同。
(vkenv) shami@ubuntu-web01:~$ ~/vkproject/manage.py makemigrations
        No changes detected
        (vkenv) shami@ubuntu-web01:~$ ~/vkproject/manage.py migrate
        Operations to perform:
          Apply all migrations: admin, auth, contenttypes, pages, sessions
        Running migrations:
          Applying contenttypes.0001_initial...Traceback (most recent call last):
          File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py",
     

第82行,在_execute中                   返回self.cursor.execute(sql)               psycopg2.errors.DuplicateTable:关系“ django_content_type”已经存在

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

        Traceback (most recent call last):
          File "/home/shami/vkproject/manage.py", line 21, in <module>
            main()
          File "/home/shami/vkproject/manage.py", line 17, in main
            execute_from_command_line(sys.argv)
          File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
            utility.execute()
          File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
            self.fetch_command(subcommand).run_from_argv(self.argv)
          File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
            self.execute(*args, **cmd_options)
          File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
            output = self.handle(*args, **options)
          File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
            res = handle_func(*args, **kwargs)
          File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
            fake_initial=fake_initial,
          File "/home/shami/vkproject/vkenv/lib/python3.6/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 "/home/shami/vkproject/vkenv/lib/python3.6/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 "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
            state = migration.apply(state, schema_editor)
          File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
            operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
          File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/migrations/operations/models.py", line 92, in database_forwards
            schema_editor.create_model(model)
          File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 307, in create_model
            self.execute(sql, params or None)
          File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 137, in execute
            cursor.execute(sql, params)
          File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
            return super().execute(sql, params)
          File "/home/shami/vkproject/vkenv/lib/python3.6/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/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
            return executor(sql, params, many, context)
          File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
            return self.cursor.execute(sql, params)
          File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
            raise dj_exc_value.with_traceback(traceback) from exc_value
          File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute
            return self.cursor.execute(sql)
        django.db.utils.ProgrammingError: relation "django_content_type" already exists

        (vkenv) shami@ubuntu-web01:~$    
        (vkenv) shami@ubuntu-web01:~$ ~/vkproject/manage.py showmigrations
        admin
         [ ] 0001_initial
         [ ] 0002_logentry_remove_auto_add
         [ ] 0003_logentry_add_action_flag_choices
        auth
         [ ] 0001_initial
         [ ] 0002_alter_permission_name_max_length
         [ ] 0003_alter_user_email_max_length
         [ ] 0004_alter_user_username_opts
         [ ] 0005_alter_user_last_login_null
         [ ] 0006_require_contenttypes_0002
         [ ] 0007_alter_validators_add_error_messages
         [ ] 0008_alter_user_username_max_length
         [ ] 0009_alter_user_last_name_max_length
         [ ] 0010_alter_group_name_max_length
         [ ] 0011_update_proxy_permissions
        contenttypes
         [ ] 0001_initial
         [ ] 0002_remove_content_type_name
        pages
         [ ] 0001_initial
         [ ] 0002_auto_20190504_1638
         [ ] 0003_auto_20190504_1645
         [ ] 0004_auto_20190504_1651
         [ ] 0005_auto_20190506_1451
         [ ] 0006_auto_20190506_1558
         [ ] 0007_auto_20190506_1803
         [ ] 0008_auto_20190508_1455
         [ ] 0009_auto_20190508_1554
         [ ] 0010_rim_banners
         [ ] 0011_rim_banners_link_id
         [ ] 0012_auto_20190520_1137
         [ ] 0013_auto_20190520_1144
         [ ] 0014_rim_transactions_cj
         [ ] 0015_auto_20190617_1756
         [ ] 0016_auto_20190619_1433
         [ ] 0017_rim_cj_feed
        sessions
         [ ] 0001_initial
        (vkenv) shami@ubuntu-web01:~$ 
            
        (vkenv) shami@ubuntu-web01:~$ ~/vkproject/manage.py --version
        2.2.7

我很高兴有人可以在这里提供线索。预先感谢。

答案 1 :(得分:0)

我在Ubuntu 18.04 + Postgres 10.10版本上托管的Django 2.2.7或Django 3.0上解决了此问题。

>         Restore the database in Postgres database (used pgAdmin tool for this)
>         (virtualenv)python manage.py loaddata dumpfile.json
>         Dropping django_migrations table from database (used pgAdmin tool for this)
>         (virtualenv)python manage.py makemigrations
>         (virtualenv)python manage.py migrate --fake
>         (virtualenv)python manage.py migrate
>         (virtualenv)python manage.py collectstatic
>         (virtualenv)python manage.py runserver 0.0.0.0:8000

干杯!