如何更新不可更新的视图?

问题描述 投票:0回答:1

我正在研究odoo版本11.我创建了一个名为test的视图。我升级了模块。后来我需要做一些更改,包括更改一些值和删除一些值。每当我尝试升级模块时,我都会收到错误

psycopg2.OperationalError:无法从视图中删除“test”DETAIL:包含GROUP BY的视图不能自动更新。提示:要从视图中启用删除,请提供INSTEAD OF DELETE触发器或无条件ON DELETE DO INSTEAD规则。

如何提供规则?我的代码如下

@api.model_cr
def init(self):
    cr = self.env.cr   
    tools.drop_view_if_exists(cr, 'o_test')
    cr.execute("""

                CREATE or replace view o_test as 
                (
                    SELECT 
                        mve.id as id,
                        acc.code as account_code,
                        SUM (mve.debit-mve.credit) AS balance,
                        mve.account_id as account_id
                    FROM account_move_line mve
                    LEFT JOIN account_account acc ON mve.account_id = acc.id 
                    GROUP BY mve.id,mve.account_id,acc.code
                )
            """)

我想将代码更改为如下:

@api.model_cr
def init(self):
    cr = self.env.cr   
    tools.drop_view_if_exists(cr, 'o_test')
    cr.execute("""

                CREATE or replace view o_test as 
                (
                    SELECT 
                        mve.account_id as id,
                        acc.code as account_code,
                        SUM (mve.debit-mve.credit) AS balance,
                        mve.account_id as account_id
                    FROM account_move_line mve
                    LEFT JOIN account_account acc ON mve.account_id = acc.id 
                    GROUP BY mve.account_id,acc.code
                )
            """)
python-3.x postgresql psycopg2 odoo-11
1个回答
1
投票

可以使用规则在Postgres中创建可编辑视图,GROUP BY子句应该不是问题。您可以编写如下所示的规则以进行更新查询。在这里,我只更新了一列

CREATE OR REPLACE RULE update_o_test AS
    ON UPDATE TO o_test
    DO INSTEAD
 UPDATE account_account SET code = new.code
  WHERE account_account.id = old.id;

同样,您可以为插入和删除操作创建规则。在create view语句结束后添加规则应该有效。希望这可以帮助...

© www.soinside.com 2019 - 2024. All rights reserved.