Django:ProgrammingError关系不存在

时间:2016-12-05 18:15:37

标签: django heroku django-migrations django-postgresql

要在heroku上设置新数据库,我尝试了python manage migrate并获得了许多与relation already exists/does not exists相关的例外情况。所以我按照django 1.9: ProgrammingError: relation "users_user" does not exist中的说明操作,但它没有用。

~ $ django-admin showmigrations
admin
 [ ] 0001_initial
 [ ] 0002_logentry_remove_auto_add
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
contenttypes
 [ ] 0001_initial
 [ ] 0002_remove_content_type_name
dashboard
 [ ] 0001_squashed_0024_auto_20161205_2214 (24 squashed migrations)
sessions
 [ ] 0001_initial

当我运行migrate

~ $ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, dashboard, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying dashboard.0001_squashed_0024_auto_20161205_2214...Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "dashboard_dashboards" does not exist


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 "/app/.heroku/python/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.5/site-packages/django/core/management/base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.5/site-packages/django/core/management/base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/app/.heroku/python/lib/python3.5/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 "/app/.heroku/python/lib/python3.5/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 "/app/.heroku/python/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 92, in __exit__
    self.execute(sql)
  File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 112, in execute
    cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/app/.heroku/python/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "dashboard_dashboards" does not exist

以下是0001_squashed_0024_auto_20161205_2214.py

的相关部分
operations = [
        migrations.CreateModel(
            name='Dashboards',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('dashboard_name', models.CharField(max_length=200, unique=True, verbose_name='Dashboard Name')),
                ('create_date', models.DateTimeField(auto_now=True, verbose_name='Dashboard Create Date')),
                ('advisory_end_date', models.DateField(verbose_name='End date of Advisory Process')),
                ('advisory_start_date', models.DateField(verbose_name='Start date of Advisory Process')),
                ('number_of_teams', models.IntegerField(verbose_name='Number of teams')),
            ],
        ),
        migrations.AlterModelOptions(
            name='dashboards',
            options={'verbose_name': 'Dashboard', 'verbose_name_plural': 'Dashboards'},
        ),
        migrations.RenameModel(
            old_name='Dashboards',
            new_name='Dashboard',
        ),
        migrations.RenameField(
            model_name='dashboard',
            old_name='dashboard_name',
            new_name='name',
        ),
        migrations.AlterField(
            model_name='dashboard',
            name='create_date',
            field=models.DateTimeField(auto_now_add=True, verbose_name='Dashboard Create Date'),
        ),

使用sqlite3数据库在本地运行迁移时,迁移可以正常运行,但heroku postgresql不能运行。

2 个答案:

答案 0 :(得分:1)

我自己也遇到过类似的问题。压缩迁移的问题是某些表尚未创建,但django无论如何都试图访问它们以创建关系。

你可以做两件事

1)不要挤压迁移

2)删除迁移中的关系。运行makemigration以单独创建关系并在之后迁移它。

我不确定你必须删除哪一行。这对我来说不是很清楚。可能是以下三种选择之一,其中一些或全部。

    migrations.RenameModel(
        old_name='Dashboards',
        new_name='Dashboard',
    ),

    migrations.AlterModelOptions(
        name='dashboards',
        options={'verbose_name': 'Dashboard', 'verbose_name_plural': 'Dashboards'},
    ),

    migrations.AlterField(
        model_name='dashboard',
        name='create_date',
        field=models.DateTimeField(auto_now_add=True, verbose_name='Dashboard Create Date'),
    ),

因为它试图改变一个尚未创建的模型。

答案 1 :(得分:1)

这可能对部署到Heroku很有帮助。 https://ultimatedjango.com/learn-django/lessons/push-to-heroku/


问题在于,在Heroku上,您需要先运行迁移,然后再运行服务器,因此这对我有用。

$ heroku run python manage.py migrate