使用django-csvimport的项目迁移错误

时间:2017-11-20 23:50:22

标签: python django python-3.x

我无法将Django 1.11应用程序移至生产环境。

还有另一个看似类似问题的问题,但我无法根据需要在评论中提出建议: Django import errors for app csvimport

如果我注释掉settings.py中的代码,就可以删除django-csvimport库:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
   # 'csvimport.app.CSVImportConf',
    'custom_app_name',

]

然后我的迁移工作正常,应用程序运行(没有csvimport应用程序)。然后,如果我重新评论csvimport APP行并运行迁移,则会失败并显示以下内容:

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, csvimport, sessions, custom_app_name
Running migrations:
  Applying csvimport.0002_test_models...Traceback (most recent call last):
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: syntax error at or near "csvtests_country"
LINE 1: ...CONSTRAINT "csvtests_item_country_id_5f8b06b9_fk_"csvtests_c...
                                                             ^


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

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/migrations/executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 93, in __exit__
    self.execute(sql)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 120, in execute
    cursor.execute(sql, params)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/backends/utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/www-root/envs/django_env_1/lib/python3.4/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: syntax error at or near "csvtests_country"
LINE 1: ...CONSTRAINT "csvtests_item_country_id_5f8b06b9_fk_"csvtests_c...

任何建议都会非常有用 谢谢!

1 个答案:

答案 0 :(得分:2)

所以我能够根据堆栈跟踪错误解决这个问题:

django.db.utils.ProgrammingError: syntax error at or near 
"csvtests_country"
LINE 1: ...CONSTRAINT "csvtests_item_country_id_5f8b06b9_fk_"csvtests_c...

问题在于,有一段生成的代码在单引号中添加了双引号,如下所示:

 options={
                'managed': True,
                'db_table': '"csvtests_country"',
            },

要查找迁移,我访问了我的虚拟环境的python install:

django_env/lib/python3.4/site-packages/csvimport/migrations/

并删除双引号,因为没有其他的&#39; db_table&#39; :&#39; sometable_name&#39;调用使用双引号。我想这是一个我将向csvimport github页面报告的错误。如果我找到更多信息,我会发布此问题的任何更新。

希望这有助于处于类似情况的人。

谢谢!

更新:

我之前在csvimport github页面的问题列表中没有看到这个。它似乎是一个已知的错误: https://github.com/edcrewe/django-csvimport/issues/77