典型的“关系" auth_user不存在"与PSQL和Django 2 [英] Typical "relation "auth_user" does not exist" with PSQL and Django 2

查看:152
本文介绍了典型的“关系" auth_user不存在"与PSQL和Django 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已解决

我遇到了像creator = models.ForeignKey(User, on_delete=models.CASCADE, default=User.objects.first().id)这样的行,这就是问题所在. 当我将其更改为creator = models.ForeignKey(User, on_delete=models.CASCADE)时,所有内容又重新开始工作.

I had a line like creator = models.ForeignKey(User, on_delete=models.CASCADE, default=User.objects.first().id) that was the problem. When I changed it to creator = models.ForeignKey(User, on_delete=models.CASCADE) all started to work again.

谢谢.

我知道这是一个经常性的问题,可以通过迁移解决,但不是我的情况(我认为).

I know that it is a recurrent ask, which it's solutioned with migrations, but not my case (I think).

我有一个Django项目(我已经尝试使用Django 2.0、2.1和2.1.1) 有一个db.sqlite3并正常工作.现在,我尝试使用具有以下配置的PostgreSQL 10来切换数据库:

I have a Django project (I've tried with Django 2.0, 2.1 and 2.1.1) that had a db.sqlite3 and worked fine. Now, I've tried to switch the database with PostgreSQL 10 with those configurations:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql', #I've already tested with PostgreSQL_psycopg2
        'NAME': 'mydbs',
        'USER': 'niknitro',
        'PASSWORD': 'password',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

在pg_hba.conf中,我添加了这一行:

In pg_hba.conf, I added this line:

host    all             all             0.0.0.0/32              trust

当我尝试执行python manage.py migratepython manage.py makemigrationspython manage.py runserver时,它是相同的错误:

And when I try to do a python manage.py migrate, python manage.py makemigrations or python manage.py runserver, it's the same error:

Starting myproject execution...
/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
  """)
Starting myproject execution...
/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
  """)
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f4ce0070e18>
Traceback (most recent call last):
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "auth_user" does not exist
LINE 1: ...user"."is_active", "auth_user"."date_joined" FROM "auth_user...
                                                             ^


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

Traceback (most recent call last):
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 248, in raise_last_exception
    raise _exception[1]
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 337, in execute
    autoreload.check_errors(django.setup)()
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
    app_config.import_models()
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/root/PycharmProjects/myproject/WiFiNets/models.py", line 5, in <module>
    class Wifi(models.Model):
  File "/root/PycharmProjects/myproject/WiFiNets/models.py", line 17, in Wifi
    creator = models.ForeignKey(User, on_delete=models.CASCADE, default=User.objects.first().id)
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/db/models/query.py", line 605, in first
    for obj in (self if self.ordered else self.order_by('pk'))[:1]:
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/db/models/query.py", line 268, in __iter__
    self._fetch_all()
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/db/models/query.py", line 1186, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/db/models/query.py", line 54, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1065, in execute_sql
    cursor.execute(sql, params)
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/root/PycharmProjects/myproject/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "auth_user" does not exist
LINE 1: ...user"."is_active", "auth_user"."date_joined" FROM "auth_user...

也在PostgreSQL.log中,出现以下行:

Also in PostgreSQL.log, appears this line:

2018-09-01 14:23:56.064 UTC [22027] niknitro@myproject ERROR:  relation "auth_user" does not exist at character 280
2018-09-01 14:23:56.064 UTC [22027] niknitro@myproject STATEMENT:  SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user" ORDER BY "auth_user"."id" ASC  LIMIT 1

我知道此错误通常是通过makemigrations和migration修复的,但不是这种情况. 我也尝试删除了迁移,但是没有用.

I know that this error is usually fixed with a makemigrations and a migrate but not this case. I also tried to remove the migrations but didn't works.

这是我的要求:

amqp==2.3.2
anyjson==0.3.3
billiard==3.5.0.4
celery==4.2.1
certifi==2018.4.16
chardet==3.0.4
Django==2.1.1
django-bootstrap3==9.1.0
django-celery==3.2.2
django-forms-bootstrap==3.1.0
django-rest-framework==0.1.0
django-tables2==1.21.2
djangorestframework==3.8.2
idna==2.7
kombu==4.2.1
psycopg2==2.7.5
psycopg2-binary==2.7.5
pytz==2017.3
requests==2.19.1
urllib3==1.23
vine==1.1.4

如果需要,请让我分享更多信息.

Please, ask me to share more information if you need it.

非常感谢您

推荐答案

您不能具有涉及数据库查询的默认值.这将在首次导入时执行空中航线,因此创建表的迁移将永远不会有运行的机会.删除默认值.

You can't have a default that involves a query on the database. That will airways be executed on first import, do the migration that creates the table will never have a chance to run. Remove the default.

这篇关于典型的“关系" auth_user不存在"与PSQL和Django 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆