gpt4 book ai didi

python - Django 测试 : Got an error creating the test database: database "database_name" already exists

转载 作者:太空狗 更新时间:2023-10-30 02:36:07 26 4
gpt4 key购买 nike

我遇到了测试问题。这是我第一次编写测试,但遇到了问题。

我刚刚在我的应用 users 中创建了一个测试文件夹,以及用于测试 url 的 test_urls.py

当我输入时:

python manage.py 测试用户

它说:

Creating test database for alias 'default'... Got an error creating the test database: database "database_name" already exists

Type 'yes' if you would like to try deleting the test database 'database_name', or 'no' to cancel:

这是什么意思?如果我输入 yes 会怎样?我会丢失数据库中的所有数据吗?

最佳答案

测试时,Django 会创建一个测试数据库来处理,这样您的开发数据库就不会受到污染。错误消息表明 Django 正在尝试创建一个名为 "database_name" 的测试数据库,并且该数据库已经存在。您应该检查您正在使用的数据库软件的表,并检查 database_name 中的内容,它可能是错误创建的。

如果您键入 yes,数据库 database_name 将被删除并且您不太可能恢复数据。因此,请先尝试了解发生了什么。

您应该在settings.py 中设置测试数据库的名称。有一个特定的 TEST dictionaryDATABASE 设置中:

settings.py

...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'USER': 'mydatabaseuser',
'NAME': 'mydatabase',
'TEST': {
'NAME': 'mytestdatabase',
},
},
}
...

默认情况下,前缀 test_ 被添加到开发数据库的名称中。你应该检查你的 settings.py 来检查发生了什么。

来自docs :

The default test database names are created by prepending test_ to the value of each NAME in DATABASES. When using SQLite, the tests will use an in-memory database by default (i.e., the database will be created in memory, bypassing the filesystem entirely!). The TEST dictionary in DATABASES offers a number of settings to configure your test database. For example, if you want to use a different database name, specify NAME in the TEST dictionary for any given database in DATABASES.

关于python - Django 测试 : Got an error creating the test database: database "database_name" already exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55685341/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com