我有以下情况:
django
管理。因此,使用managed=False
触发API端点时,会发生两件奇怪的事情:
对数据库的请求失败,并显示:
错误:在字符673上不存在关系“ consumption_recentconsumption”
(我已在postgres
级别启用日志记录,并且将完全相同的请求复制粘贴到数据库控制台客户端中可以正常工作,无需任何修改)
django
设置可以控制此设置? (我仅使用curl
手动将请求发送到API一次)这是我的模特
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
,但是在直接针对数据库运行时效果很好。
直接运行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
答案 0 :(得分:0)
回答自己,以防将来对某人有帮助。
我正在CREATE VIEW
中运行PyCharm
命令,该命令似乎对所有操作都使用事务。这意味着该视图在PyCharm
的db会话中可用(因为该视图将事务用于所有请求),但不能在外部使用。 django
应用程序正在控制台中运行,并且看不到该视图。
解决方案只是在PyCharm
中提交事务,以使其完全可见。
最终的解决方案是通过django迁移创建视图。