如何修复错误:django.db.utils.NotSupportedError:不支持的 URIs

7

我在Linux机器(AWS中)的Django项目中使用:

  • Python 3.5.1
  • Django 1.11.7

我为我的项目创建了虚拟环境,并且所有依赖项都已经完美地安装在那里。关于数据库,我使用sqlite3。版本详情如下。

>>>import sqlite3
>>>sqlite3.version
'2.6.0'
>>>sqlite3.sqlite_version_info
(3, 7, 17)

settings.py 文件中,DATABASES 部分如下:
DATABASES = {
   'default': {
      'ENGINE': 'django.db.backends.sqlite3',
      'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
   }
}

当我运行runserver时,出现了一个异常,sqlite3.NotSupportedError: URIs not supported ,这导致了一个错误django.db.utils.NotSupportedError: URIs not supported,我无法修复它。

我已经阅读了像djangoproject.comgoogle.forum这样的文章,但仍然无法理解此错误的原因。 我还尝试执行python manage.py makemigrations,但同样的错误也出现了。

注意:在Windows机器上,我的项目可以正常运行。

请参见下面的跟踪:

# python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
Unhandled exception in thread started by <function check_errors. 
<locals>.wrapper at 0x7f07ff09c2f0>
Traceback (most recent call last):
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/tech/poc/env/lib/python3.5/site- packages/django/db/backends/base/base.py", line 189, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 198, in get_new_connection
    conn = Database.connect(**conn_params)
  sqlite3.NotSupportedError: URIs not supported

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

  Traceback (most recent call last):
  File "/tech/poc/env/lib/python3.5/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/tech/poc/env/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 128, in inner_run
    self.check_migrations()
  File "/tech/poc/env/lib/python3.5/site-packages/django/core/management/base.py", line 422, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/migrations/executor.py", line 20, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/migrations/loader.py", line 52, in __init__
    self.build_graph()
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/migrations/loader.py", line 209, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
    self.ensure_schema()
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/migrations/recorder.py", line 52, in ensure_schema
    if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/base/base.py", line 254, in cursor
    return self._cursor()
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/base/base.py", line 229, in _cursor
    self.ensure_connection()
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/tech/poc/env/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/base/base.py", line 189, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 198, in get_new_connection
    conn = Database.connect(**conn_params)
django.db.utils.NotSupportedError: URIs not supported

请帮助理解并修复此错误。在堆栈中没有与此问题相关的帖子,也没有在谷歌上找到任何相关信息。


请检查 BASE_DIR 设置中指定的路径。 - AR7
有人针对这个问题有任何想法吗?是什么原因以及如何修复它?请告知。 - iPaul
4
我有同样的问题,在我的电脑上出了故障,但是我的所有同事都没有遇到这个错误。我在 Django 的代码中进行了调试,并发现在 django.db.backends.sqlite3.base 大约在第 194 行设置了 uriTrue。如果我将其注释掉,它就可以工作了。 - leech
3个回答

2
我有一个类似的问题。原来 Django 2.1 不兼容 sqlite 3.6.20。降级到 2.0.x 版本应该可以解决问题。这个问题已经被提出并标记为“关闭错误-不修复” 在这里

1

1. 首先要检查sqlite3的版本,应该是3.X。我的配置是Python 3.7django 2.1.7sqlite3。你可以按照以下方式检查sqlite3的版本:>>import sqlite3

>>sqlite3.version '2.6.0' >>> sqlite3.sqlite_version_info (3, 6, 20)

  1. "/usr/local/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py"中查找行"kwargs.update({'check_same_thread': False, 'uri': False})"。如果'uri'为True,则将其更改为False作为root。代码更改后,重新启动计算机并运行python runserver migrate。这应该解决您的问题。

此外,您还可以尝试在SETTINGS.py文件中使用硬编码的db.sqli3文件名更改DATABASE['NAME']。


0

我在连接Ubuntu上的Postgresql时遇到了同样的问题。我将Django版本从最新版本降级到3.2.15,这解决了我的问题。希望这能帮到你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接