【发布时间】:2016-11-08 21:06:53
【问题描述】:
我已经看过很多关于这个的帖子,但到目前为止没有一个对我有帮助。我有一个工作的 django 应用程序,我想从 sqlite 转到 postgresql 数据库。为此,我正在关注 djangogirls 教程,这很棒,直到出现一些错误。
我已经在我的电脑上下载了 postgresql 进行开发,然后我下载并安装了运行良好的 psycopg2,我更改了我的 settings.py 数据库配置,我正处于我想要迁移到 postgres 的地步。
他们在教程中所说的只是运行“python manage.py migrate”,这对我不起作用,可能是因为他们认为这是一个空白应用程序或其他东西,而我的已经有与之相关的迁移和模型。
我读到了运行
python manage.py makemigrations
python manage.py migrate
可以解决问题,但我的自定义模型有问题。然后我读到我可以注释掉所有使用这个有问题的模型的代码,然后运行
python manage.py makemigrations
python manage.py migrate --fake
取消注释,然后运行
python manage.py migrate
我试过了,但现在我得到了与我的自定义模型相同的错误,但对于 contenttypes 应用程序。
回溯是这样的:
("la 关系 'django_content_type' n'existe pas" = "关系 'django_content_type' 不存在")
Operations to perform:
Apply all migrations: auth, contenttypes, sessions, admin
Running migrations:
No migrations to apply.
Traceback (most recent call last):
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: ERREUR: la relation « django_content_type » n'existe pas
LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\contrib\contenttypes\models.py", line 67, in get_for_model
ct = self.get(app_label=opts.app_label, model=opts.model_name)
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\models\manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\models\query.py", line 381, in get
num = len(clone)
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\models\query.py", line 240, in __len__
self._fetch_all()
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\models\query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\models\query.py", line 52, in __iter__
results = compiler.execute_sql()
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\models\sql\compiler.py", line 848, in execute_sql
cursor.execute(sql, params)
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ERREUR: la relation « django_content_type » n'existe pas
LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co...
^
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
utility.execute()
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\core\management\__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\core\management\base.py", line 399, in execute
output = self.handle(*args, **options)
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
emit_post_migrate_signal(self.verbosity, self.interactive, connection.alias)
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\core\management\sql.py", line 50, in emit_post_migrate_signal
using=db)
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\dispatch\dispatcher.py", line 192, in send
response = receiver(signal=self, sender=sender, **named)
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\contrib\auth\management\__init__.py", line 85, in create_permissions
ctype = ContentType.objects.db_manager(using).get_for_model(klass)
File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\contrib\contenttypes\models.py", line 80, in get_for_model
"Error creating new content types. Please make sure contenttypes "
RuntimeError: Error creating new content types. Please make sure contenttypes is migrated before trying to migrate apps individually.
我的迁移如下所示:
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
sessions
[X] 0001_initial
我不知道如何解决这个问题,因为我尝试了很多东西。数据库和部署不是我的强项。有人可以帮我解决这个问题吗?
编辑 我尝试将所有更改还原到我的应用程序工作的部分,并通过删除我的 sqlite 文件和所有迁移重新开始,我删除了所有 pyc(python 缓存)文件,注释掉了与 Ecole 模型相关的所有代码,然后再次迁移,我仍然遇到同样的错误:(
【问题讨论】:
-
您想保留来自 sqlite 的数据吗?
python manage.py migrate contenttypes是做什么的? -
我不介意从 sqlite 丢失我的数据,并且该命令输出与上述相同的错误:/
-
您可以编辑问题,而不是在答案中添加新信息。
标签: python django postgresql sqlite database-migration