【发布时间】:2018-12-13 10:58:40
【问题描述】:
我有以下场景:
- 我已经在数据库中定义了正确的视图,注意视图是根据 django 约定命名的
- 我已确定我的模型不是由
django管理的。相应地使用managed=False定义创建的迁移 - DB 视图本身运行良好。
在触发 API 端点时,会发生两件奇怪的事情:
-
对数据库的请求失败:
错误:关系“consumption_recentconsumption”在字符 673 处不存在
(我在postgres 级别启用了日志记录,并且将完全相同的请求复制粘贴到数据库控制台客户端可以正常工作,无需任何修改)
- 对数据库的请求被重试了很多次(超过 30 次?)。为什么会这样?是否有
django设置来控制它? (我只向 API 发送一次请求,手动使用curl)
编辑
这是我的模型:
class RecentConsumption(models.Model):
name = models.CharField(max_length=100)
...
class Meta:
managed = False
这是SQL 语句,由django 生成并发送到数据库:
SELECT "consumption_recentconsumption"."id", "consumption_recentconsumption"."name", ... FROM "consumption_recentconsumption" LIMIT 21;
正如我所提到的,这通过 django 失败,但在直接针对 db 运行时工作正常。
编辑2
直接运行 sql 时来自 postgres 的日志:
2018-12-13 11:12:02.954 UTC [66] LOG: execute <unnamed>: SAVEPOINT JDBC_SAVEPOINT_4
2018-12-13 11:12:02.955 UTC [66] LOG: execute <unnamed>: SELECT "consumption_recentconsumption"."id", "consumption_recentconsumption"."name", "consumption_recentconsumption"."date", "consumption_recentconsumption"."psc", "consumption_recentconsumption"."material", "consumption_recentconsumption"."system", "consumption_recentconsumption"."env", "consumption_recentconsumption"."objs", "consumption_recentconsumption"."size", "consumption_recentconsumption"."used", "consumption_recentconsumption"."location", "consumption_recentconsumption"."WWN", "consumption_recentconsumption"."hosts", "consumption_recentconsumption"."pool_name", "consumption_recentconsumption"."storage_name", "consumption_recentconsumption"."server" FROM "consumption_recentconsumption" LIMIT 21
2018-12-13 11:12:10.038 UTC [66] LOG: execute <unnamed>: RELEASE SAVEPOINT JDBC_SAVEPOINT_4
通过 django 运行时来自 postgres 的日志(重复超过 30 次):
2018-12-13 11:13:50.782 UTC [75] LOG: statement: SELECT "consumption_recentconsumption"."id", "consumption_recentconsumption"."name", "consumption_recentconsumption"."date", "consumption_recentconsumption"."psc", "consumption_recentconsumption"."material", "consumption_recentconsumption"."system", "consumption_recentconsumption"."env", "consumption_recentconsumption"."objs", "consumption_recentconsumption"."size", "consumption_recentconsumption"."used", "consumption_recentconsumption"."location", "consumption_recentconsumption"."WWN", "consumption_recentconsumption"."hosts", "consumption_recentconsumption"."pool_name", "consumption_recentconsumption"."storage_name", "consumption_recentconsumption"."server" FROM "consumption_recentconsumption" LIMIT 21
2018-12-13 11:13:50.783 UTC [75] ERROR: relation "consumption_recentconsumption" does not exist at character 673
2018-12-13 11:13:50.783 UTC [75] STATEMENT: SELECT "consumption_recentconsumption"."id", "consumption_recentconsumption"."name", "consumption_recentconsumption"."date", "consumption_recentconsumption"."psc", "consumption_recentconsumption"."material", "consumption_recentconsumption"."system", "consumption_recentconsumption"."env", "consumption_recentconsumption"."objs", "consumption_recentconsumption"."size", "consumption_recentconsumption"."used", "consumption_recentconsumption"."location", "consumption_recentconsumption"."WWN", "consumption_recentconsumption"."hosts", "consumption_recentconsumption"."pool_name", "consumption_recentconsumption"."storage_name", "consumption_recentconsumption"."server" FROM "consumption_recentconsumption" LIMIT 21
【问题讨论】:
-
这个不看模型和视图的代码是无法回答的。您在哪里看到 db 调用?
-
@DanielRoseman 正如我在 postgres 日志中提到的那样
标签: django postgresql django-rest-framework