【发布时间】:2021-03-29 20:46:31
【问题描述】:
正如标题所说,我的开发环境受到限制。我无法为测试目的创建额外的数据库,只能创建本地文件。
使用django的environ包,数据库的配置是
DATABASES = {
'default': env.db('DATABASE_URL', default='postgres://name@localhost'),
}
正如在this thread 中找到的,我所需要的只是此配置中的“TEST”键。它没有一个默认值,所以我通过写作绕过了这一点
db = env.db('DATABASE_URL', default='postgres://name@localhost')
db["TEST"] = {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
但这不起作用。 python manage.py test 给我以下输出:
Creating test database for alias 'default'...
/usr/lib/python2.7/dist-packages/django/db/backends/postgresql/base.py:267: RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the default database instead.
RuntimeWarning
Got an error creating the test database: permission denied to create database
Type 'yes' if you would like to try deleting the test database '/home/<closed environment>/<project>/config/db.sqlite3', or 'no' to cancel:
它仍然尝试在我无权更改自己权限的环境中创建数据库,这当然失败了。我确实将测试数据库指定为本地文件,但不知何故不使用它?我是在监督什么,还是我做错了什么?
【问题讨论】:
-
config/db.sqlite3已经存在了吗?如果你输入“是”会发生什么? -
作为一个快速修复,也许试试
python manage.py test --keepdb?仅供参考,不测试数据库的迁移。 -
死亡....
django.db.utils.OperationalError: FATAL: database "/home/<name>/<project>/config/db.sqlite3" does not exist:\ -
您的用户是否有权在该路径创建其他文件?
touch /config/test-file? -
试试
db["default"]["TEST"]?因为TEST是default字典上的一个键。
标签: python django postgresql testing