From 17c4f47b0ae7a0eec78b5555d52eeb0bb5e3cc5a Mon Sep 17 00:00:00 2001 From: Nicolas Vannieuwerburgh Date: Fri, 22 Nov 2019 08:40:03 +0000 Subject: [PATCH] [FIX] base: allow custom model to use SQL (materialized) views instead of table closes odoo/odoo#41267 X-original-commit: f17d389c4625e95f52c299c13a3f2983127c70bc Signed-off-by: Raphael Collet (rco) Co-authored-by: Denis Ledoux Co-authored-by: Raphael Collet --- odoo/addons/base/models/ir_model.py | 8 +++++++- odoo/tools/sql.py | 14 ++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/odoo/addons/base/models/ir_model.py b/odoo/addons/base/models/ir_model.py index 6fbd0d45679..6633ff5e28f 100644 --- a/odoo/addons/base/models/ir_model.py +++ b/odoo/addons/base/models/ir_model.py @@ -314,7 +314,13 @@ class IrModel(models.Model): cr.execute('SELECT * FROM ir_model WHERE state=%s', ['manual']) for model_data in cr.dictfetchall(): model_class = self._instanciate(model_data) - model_class._build_model(self.pool, cr) + Model = model_class._build_model(self.pool, cr) + if tools.table_kind(cr, Model._table) not in ('r', None): + # not a regular table, so disable schema upgrades + Model._auto = False + cr.execute('SELECT * FROM %s LIMIT 0' % Model._table) + columns = {desc[0] for desc in cr.description} + Model._log_access = set(models.LOG_ACCESS_COLUMNS) <= columns # retrieve field types defined by the framework only (not extensions) diff --git a/odoo/tools/sql.py b/odoo/tools/sql.py index e1e4256dc7f..28cbe3ab25f 100644 --- a/odoo/tools/sql.py +++ b/odoo/tools/sql.py @@ -8,13 +8,6 @@ import psycopg2 _schema = logging.getLogger('odoo.schema') -_TABLE_KIND = { - 'BASE TABLE': 'r', - 'VIEW': 'v', - 'FOREIGN TABLE': 'f', - 'LOCAL TEMPORARY': 't', -} - _CONFDELTYPES = { 'RESTRICT': 'r', 'NO ACTION': 'a', @@ -42,11 +35,12 @@ def table_exists(cr, tablename): def table_kind(cr, tablename): """ Return the kind of a table: ``'r'`` (regular table), ``'v'`` (view), - ``'f'`` (foreign table), ``'t'`` (temporary table), or ``None``. + ``'f'`` (foreign table), ``'t'`` (temporary table), + ``'m'`` (materialized view), or ``None``. """ - query = "SELECT table_type FROM information_schema.tables WHERE table_name=%s" + query = "SELECT relkind FROM pg_class WHERE relname = %s" cr.execute(query, (tablename,)) - return _TABLE_KIND[cr.fetchone()[0]] if cr.rowcount else None + return cr.fetchone()[0] if cr.rowcount else None def create_model_table(cr, tablename, comment=None): """ Create the table for a model. """