django Assert错误:数据库连接未设置为UTC

vfwfrxfs  于 2023-01-31  发布在  Go
关注(0)|答案(7)|浏览(276)

我已经用相同的设置做了多次服务器设置,但这一次,我看到了错误消息。它甚至不允许迁移数据库。

System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/utils/autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
    self.check_migrations()
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/core/management/base.py", line 458, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/migrations/loader.py", line 49, in __init__
    self.build_graph()
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/migrations/loader.py", line 212, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
    return {(migration.app, migration.name): migration for migration in self.migration_qs}
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/query.py", line 276, in __iter__
    self._fetch_all()
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/query.py", line 1261, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/query.py", line 57, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1170, in execute_sql
    return list(result)
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1569, in cursor_iter
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1569, in <lambda>
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/utils.py", line 97, in inner
    return func(*args, **kwargs)
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/backends/postgresql/utils.py", line 6, in utc_tzinfo_factory
    raise AssertionError("database connection isn't set to UTC")
AssertionError: database connection isn't set to UTC

这是我settings.py的时区网站www.example.com。

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

操作系统:Ubuntu 21.04 Python版本:3.9.5 Django版本:3.0 PostgreSQL:13.3
我也经历了another question,但没有找到任何解决方案。有没有人可以帮助我完成这一点?我有多个服务器设置相同的代码没有改变任何东西,并工作,但这一次它不是。

oxiaedzo

oxiaedzo1#

psycopg 2版本2.9的发布导致了此错误,如GitHub问题中所述:
https://github.com/psycopg/psycopg2/issues/1293#issuecomment-862835147
Psycopg 2.9将传递给tzinfo_factory的值从int改为timedelta. Django 2.2(可能更新了,但我在2.2上)有一个偏移量== 0的检查,因为timedelta(0)!= 0,它会爆炸。
一种解决方案是在需求文件中将psycopg2(或者如果您使用的是独立包,则为psycopg2-binary)降级到2.9以下(例如psycopg2>=2.8,<2.9)。
例如,您可以使用以下命令降级到2.8.6

pip install psycopg2==2.8.6

pip install psycopg2-binary==2.8.6

如果你使用的是poetry,你可以执行poetry add psycopg2@2.8.6来将你的版本修改为2.8.6
psycopg 2版本历史记录

mqkwyuun

mqkwyuun2#

我遇到了同样的问题,我通过简单地从www.example.com文件中删除此行来修复它settings.py

USE_TZ = True
jogvjijk

jogvjijk3#

我通过升级Django而不是降级psycopg解决了这个问题。我不知道哪个版本能解决这个问题,但3.2肯定能。
已接受的答案已经过时,如果您可以选择升级Django,您应该决定不降级。

2guxujil

2guxujil4#

这就是我正在努力让这一切在Django 2.2.x上工作(它与psycopg2>=2.9.0不兼容:

brew install libpq --build-from-source
brew install openssl
brew link openssl
export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib -L/opt/homebrew/opt/libpq/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include -I/opt/homebrew/opt/libpq/include"
echo 'export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"' >> ~/.zshrc
brew install postgres
pip install psycopg2==2.8.6

我在一台M1 MacBook上使用BigSur。

htzpubme

htzpubme5#

对于ubuntu用户来说,在旧版本的django上安装psycopg 2 ==2.8.6,psycopg 2-binary==2.8.6可能会引发某种错误。要解决这个问题,请尝试,

sudo apt-get安装gcc libpq-dev -y
sudo apt-获取安装python3-开发python3-pip python3-venv python3-轮-y
pip 3安装轮

这是一种解决方案,其他解决方案可在下面的链接中找到。https://exerror.com/error-invalid-command-bdist_wheel/#:%7E:text=正好%20use%20below%20command%20pip,command%20python%20setup.py%20bdist_wheel.& text =无效%20命令%20%27bdist_wheel %27-,要%20解决%20错误%3A %20无效%20命令%20%27bdist_wheel %27%20错误%20您%20正好,命令%20python%20setup.py%20bdist_wheel.?

bjg7j2ky

bjg7j2ky6#

如果您已停用虚拟环境,请确保您已将其激活(请确保您处于虚拟环境中)
要激活虚拟环境,请使用以下命令:虚拟环境的源名称/bin/activate

noj0wjuj

noj0wjuj7#

我有这个问题..通过更新django最新版本解决. INSTALLED_APPS = [ 'cart',]这一行导致我的错误时,较低版本(2.2)是使用你也可以解决这个问题,通过改变上面的代码行INSTALLED_APPS = [ 'cart.apps. CartConfig',
]
CartConfig是您可以从同一应用程序的www.example.com复制和粘贴的类名apps.py

相关问题