Django loaddata returns a permission denied for relation

China☆狼群 提交于 2021-02-16 18:24:05

问题


I am trying to load some data from a json file generated from a dumpdata on a django project to a new one with a new database using loaddata. The connection to the db seems to work but almost right away I receive a permission denied for relation django_content_type. I don't get what permission we are talking about. the db is postgres on a separate server and I connect to it through a vagrant virtual machine on my laptop that has a virtual environment and the settings.py has the settings to connect to the db. Here is the Traceback:

Problem installing fixture 'djgprd1_dumpdata.json': Traceback (most recent call last):
  File "/home/vagrant/devenv/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 196, in handle
    obj.save(using=using)
  File "/home/vagrant/devenv/local/lib/python2.7/site-packages/django/core/serializers/base.py", line 165, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "/home/vagrant/devenv/local/lib/python2.7/site-packages/django/db/models/base.py", line 524, in save_base
    manager.using(using).filter(pk=pk_val).exists())):
  File "/home/vagrant/devenv/local/lib/python2.7/site-packages/django/db/models/query.py", line 565, in exists
    return self.query.has_results(using=self.db)
  File "/home/vagrant/devenv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 441, in has_results
    return bool(compiler.execute_sql(SINGLE))
  File "/home/vagrant/devenv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 818, in execute_sql
    cursor.execute(sql, params)
  File "/home/vagrant/devenv/local/lib/python2.7/site-packages/django/db/backends/util.py", line 40, in execute
    return self.cursor.execute(sql, params)
  File "/home/vagrant/devenv/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 52, in execute
    return self.cursor.execute(query, args)
DatabaseError: Could not load contenttypes.ContentType(pk=29): permission denied for relation django_content_type

What am I doing wrong here?


回答1:


The postgres user for this db was created automatically and I did not check the permissions, they were missing for this db so a GRANT ALL PRIVILEGES ON DATABASE x for user y; resolved the permissions problem. My bad.




回答2:


Looks like you're exporting ContentType objects (did you dumpdata with --all?), is this by design?

The docs mention that you might want to use the --natural flag, that fixes problems such as this, see https://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-option---natural

Also see this quote:

If you’re serializing data (for example, when generating fixtures) from a model that implements generic relations, you should probably be using a natural key to uniquely identify related ContentType objects. See natural keys and dumpdata --natural for more information.

Source: https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/



来源:https://stackoverflow.com/questions/19222373/django-loaddata-returns-a-permission-denied-for-relation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!