使用Heroku

时间:2017-10-11 03:17:22

标签: django postgresql heroku

尝试在Heroku上使用django迁移数据库更改时,我得到:

psycopg2.ProgrammingError: permission denied for relation django_migrations

其他人可以通过授予适当的权限来解决此问题(例如Permission denied for relation)。不幸的是,我无法在Heroku爱好层上授予权限或创建新用户。

我能够回滚一些迁移,但最终我得到了:

django.db.utils.ProgrammingError: table "labs_branch_tests" does not exist

并且无法迁移回较新的迁移。

关系django_migrations

的权限已完全追溯
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/app/.heroku/python/lib/python3.6/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.6/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.6/site-packages/django/db/migrations/executor.py", line 250, in apply_migration
    self.recorder.record_applied(migration.app_label, migration.name)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 73, in record_applied
    self.migration_qs.create(app=app, name=name)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 394, in create
    obj.save(force_insert=True, using=self.db)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 807, in save
    force_update=force_update, update_fields=update_fields)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 837, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 923, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 962, in _do_insert
    using=using, raw=raw)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 1076, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1107, in execute_sql
    cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/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.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: permission denied for relation django_migrations

psycopg2.ProgrammingError的完整追溯:table&#34; labs_branch_tests&#34;不存在

  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 119, in migrate
    state = self._migrate_all_backwards(plan, full_plan, fake=fake)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 194, in _migrate_all_backwards
    self.unapply_migration(states[migration], migration, fake=fake)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 264, in unapply_migration
    state = migration.unapply(state, schema_editor)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/migration.py", line 178, in unapply
    operation.database_backwards(self.app_label, schema_editor, from_state, to_state)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 95, in database_backwards
    schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 457, in remove_field
    return self.delete_model(field.remote_field.through)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 324, in delete_model
    "table": self.quote_name(model._meta.db_table),
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 120, in execute
    cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/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.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: table "labs_branch_tests" does not exist

3 个答案:

答案 0 :(得分:12)

我能够通过在postgres终端中为用户授予权限来解决这个问题。

首先通过Heroku CLI(\c)连接到postgres。

这很笨重但是通过在postgres终端中调用You are now connected to database DATABASE_NAME as user USERNAME来获取您的Heroku postgres用户名。 (输出的最后一行是GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public to USERNAME; GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public to USERNAME; GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public to USERNAME; 。)

然后像其他人所示的那样授予权限:

SQLSETPROP(0, "DispLogin", 3)

答案 1 :(得分:2)

在我的情况下,这是因为我的免费数据库达到了行数限制,并且撤销了写访问权限:

$ heroku pg:info -r heroku
=== DATABASE_URL
Plan:                  Hobby-dev
Status:                Available
Connections:           0/20
PG Version:            10.4
[..]
Rows:                  11683/10000 (Write access revoked)
[..]

答案 2 :(得分:0)

对我来说,您可以确定写访问权限已被撤销

$ heroku pg:info
...
Rows:                  11683/10000 (Write access revoked)

但是我不想转储整个数据库。相反,我用

来标识最大的表
$ heroku pg:psql
>  select
  table_name
  , pg_relation_size(quote_ident(table_name)) as "raw_size"
  , pg_size_pretty(pg_relation_size(quote_ident(table_name)) as "size"
from information_schema.tables                                                                                                                                                                                                              
where table_schema = 'public'                                                                                                                                                                                                               
order by "raw_size" desc;

然后我用清除最大的表

> delete from <table name>;