【问题标题】:Django refuses to use DB viewDjango 拒绝使用 DB 视图
【发布时间】:2018-12-13 10:58:40
【问题描述】:

我有以下场景:

  • 我已经在数据库中定义了正确的视图,注意视图是根据 django 约定命名的
  • 我已确定我的模型不是由django 管理的。相应地使用managed=False 定义创建的迁移
  • DB 视图本身运行良好。

在触发 API 端点时,会发生两件奇怪的事情:

  1. 对数据库的请求失败:

    错误:关系“consumption_recentconsumption”在字符 673 处不存在

(我在postgres 级别启用了日志记录,并且将完全相同的请求复制粘贴到数据库控制台客户端可以正常工作,无需任何修改

  1. 对数据库的请求被重试了很多次(超过 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


【解决方案1】:

回答自己,以防将来对某人有所帮助。

我在PyCharm 中运行CREATE VIEW 命令,这似乎对所有操作都使用事务。这意味着该视图在PyCharm 的数据库会话中可用(因为它对所有请求都使用事务),但不能从外部使用。 django 应用程序正在控制台中运行,并且看不到视图。

解决办法就是在PyCharm中提交事务,让它完全可见。

最终的解决方案是通过 django 迁移创建视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    相关资源
    最近更新 更多