I'm working on odoo version11. I created a view named test. I upgraded the module. Later I need to make some changes including changing some values and removing some values. Whenever I try to upgrade the module I get the error
psycopg2.OperationalError: cannot delete from view "test" DETAIL: Views containing GROUP BY are not automatically updatable. HINT: To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule.
How to provide the rule? My code is as follows
@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
)
""")
I want to change the code to as follows:
@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
)
""")
Editable Views can be created in Postgres using Rules and GROUP BY clause should not be a problem. You can write rule like the below for update query. Here I have updated only one column
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;
Similarly you can create rules for Insert and Delete operations. Adding rules after the end of your create view statement should work. Hope this helps...