断言错误:数据库连接未设置为 UTC [英] AssertionError: database connection isn't set to UTC

查看:87
本文介绍了断言错误:数据库连接未设置为 UTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用相同的设置多次完成服务器设置,但这次,我看到了错误消息.它甚至不允许迁移数据库.

I have done server setup multiple times with the same settings but this time, I am seeing the error message. It is not even allowing to migrate the database.

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.

Here is my settings.py for timezone.

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

操作系统:Ubuntu 21.04Python 版本:3.9.5Django 版本:3.0PostgreSQL:13.3

OS: Ubuntu 21.04 Python Version : 3.9.5 Django Version: 3.0 PostgreSQL: 13.3

我也经历过另一个问题 但没有找到任何解决方案.有没有人可以帮助我完成这项工作?我使用相同的代码设置了多个服务器,无需更改任何内容并且可以正常工作,但这次不是.

I have also gone through another question but did not find any solution. Is there anyone who can help me to get this done? I have multiple server setup with same code without changing anything and worked but this time it is not.

推荐答案

psycopg2 2.9 的更新导致了这个问题,如这个 GitHub 问题所述:

The update to psycopg2 2.9 is causing this issue as explained in this GitHub issue:

https://github.com/psycopg/psycopg2/issues/1293#issuecomment-862835147

Psycopg 2.9 将传递给 tzinfo_factory 的值从 int 更改为 timedelta.Django 2.2(可能更新,但我使用的是 2.2)检查了 offset == 0 并且因为 timedelta(0) != 0 它变得繁荣.

Psycopg 2.9 changed the value passed to tzinfo_factory from an int to a timedelta. Django 2.2 (possibly newer but I'm on 2.2) has a check for offset == 0 and since timedelta(0) != 0 it goes boom.

当前的解决方案是将 psycopg2(或 psycopg2-binary,如果您使用独立包)低于 2.9(例如 psycopg2>=2.8,<2.9) 在您的需求文件中.

A current solution would be to peg psycopg2 (or psycopg2-binary if you are using the stand-alone package) below 2.9 (e.g. psycopg2>=2.8,<2.9) in your requirements file.

这篇关于断言错误:数据库连接未设置为 UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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