2016-07-06 119 views
1

我已经看到了一些关于此的帖子,但目前为止没有人帮助我。我有一个工作的Django应用程序,我想从sqlite到postgresql数据库。为此,我遵循djangogirls教程,这很棒,直到发生一些错误。django sqlite到postgresql迁移时的ProgrammingError

我已经在我的pc上下载了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应用程序。

回溯是这样的:
( “拉关系 '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 

我不知道如何解决这个问题,因为我尝试了很多东西。数据库和部署不是我的优点。有人可以帮我解决这个问题吗?

编辑 我想我所有的变化恢复到我的申请工作,并通过删除我的源码文件和我所有的移民开始在部分,我删除了所有我的pyc(蟒蛇缓存)文件,注释掉所有代码与Ecole模型相关,然后再迁移,并且我仍然得到相同的错误:(

+0

你想保留你的数据从sqlite? “python manage.py migrate contenttypes”是做什么的? –

+0

我不介意从sqlite中丢失我的数据,并且该命令会输出与上述相同的错误:/ –

+0

您可以编辑问题,而不是在答案中添加新的信息。 – marcanuy

回答

0

您可以dump your data into json,删除您的sqlite文件,然后将您的项目迁移到postgres set up as a DB

工作流程示例可能类似于:

mkdir <app name>/fixtures

python manage.py dumpdata >> <app name>/fixtures/all_data.json

rm <sqlite file>.db

python manage.py migrate

python manage.py loaddata <app name>/fixtures/all_data.json

其中<app name>是您的主应用程序(或任何目录)的名称,<sqlite file>是您的sqlite文件的名称。

+0

这不再工作,然后悲伤的另一种方式:(当我尝试dumpdata,我得到一个类似于我提到的错误: 'CommandError:无法序列化数据库:错误:关系“django_content_type”doesn' t存在' –

+0

你需要保留你的数据吗 – marcusshep

+0

好吧,现在我想到了,不是真的,因为我在应用程序的加载中从Excel文件加载我的数据 –

0

那么,最后我在stackoverflow上发现了一个小帖子,说psycopg2应该在我的settings.py中的INSTALLED_APPS中。我加了它再跑一次

python manage.py migrate 

它工作的很好!感谢那些回答,但真的很感激!

虽然现在我得到了在评论出与“Ecole”模型相关的所有代码之前得到的错误。这是非常相似的contentypes一个:

programmingError: ERROR: relation "carte_interactive_ecole" does not exist 

其中“carte_interactive”是我的应用程序的名字,而“高等”我的模型。

有没有人有对我另一个想法?当有代码引用它时,为什么我无法迁移我的模型?如果我注释掉所有高等代码,这很好......

编辑
我发现,如果我注释掉的代码apps.py准备好()我可以迁移我的应用程序,但它可能不因为当我运行我的应用程序,我做了一些使用数据库的东西,我得到了与上面相同的错误。 :/

+0

尝试删除'migrations'文件夹中的所有内容,但__init __。py'文件除外。然后再次运行'python manage.py makemigrations',然后运行'python manage.py migrate'。 –

+0

没有,没有工作:/仍然一遍又一遍地得到相同的错误.. –

0

所以我张贴此作为一个答案,因为它是一个评论太长:

我apps.py注释掉的代码,使用我的模型(它可能迁移这样的模式确实之前使用它。但不存在的。然后我跑makemigrations,和我:

Migrations for 'carte_interactive': 
    001_initial.py: 
     -Create model Ecole 
     -Create model ExcelFile 

这是我的两个模型,似乎罚款

然后我跑迁移,并得到了

apply all migrations: admin, carte_interactive, [...] 
Running Migrations: No migrations to apply. 
01。

当我showmigrations,一切检查,以便一切都做似乎,虽然我昨天--fake试图所以它可能是它检查了,但并没有创造一些表...

EDIT(解决方案)
好吧,所以整个问题首先是psycopg2没有包含在settings.py中的INSTALLED_APPS中,所以无法在postgresql或其他东西中创建表。

而且,我通过彻底删除数据库并重新创建它(例如删除所有表格,但更容易),开始了迁移。然后,另一个问题是apps.py中的代码在迁移之前运行,所以我必须在运行迁移之前对其进行评论。毕竟,运行makemigrations并使用注释代码进行迁移,然后取消注释并运行该应用程序就像魅力一样工作!感谢所有回答的人,非常感谢!