【问题标题】:Why is the account module upgrade unlinking records on some tables?为什么帐户模块升级取消链接某些表上的记录?
【发布时间】:2018-12-11 21:25:10
【问题描述】:

我只是想确保升级 base 或 account 模块一切正常,因为如果我对模块进行新的更改,我想在将来升级数据库。

我在模型account.acount.type 上手动创建了新记录。但是当我尝试升级account 模块时,出现了写在这个问题底部的异常。 Odoo 正在尝试从该模型中删除记录。为什么会这样?

我已经安装了 account 模块,但是我没有从那个表中删除任何记录,我只是添加了几个类型。所以expected behaviour of upgrading modules with data is:

noupdate 表示该模块第二次升级时,该记录将不再更新。 noupdate="1" 中的记录只会在安装时初始化。

提示:如果您删除了记录,那么在下次更新系统将重新创建它。通常那些有可能从前端修改的记录,在 noupdate=1 中给出。例如,自动调度程序记录。

<data noupdate="1">
    <!-- account.account.type -->
    <record model="account.account.type" id="data_account_type_receivable">
        <field name="name">Receivable</field>
        <field name="type">receivable</field>
        <field name="include_initial_balance" eval="True"/>
    </record>

    <!-- [...]  -->

这是升级帐户模块时日志中出现的错误

2018-12-11 20:35:31,729 18018 INFO db_name odoo.addons.base.ir.ir_model: Deleting 59@account.account.type (l10n_es.account_type_third_parties)
2018-12-11 20:35:31,760 18018 ERROR db_name odoo.sql_db: bad query: b'DELETE FROM account_account_type WHERE id IN (59)'
ERROR: null value in column "user_type_id" violates not-null constraint
DETAIL:  Failing row contains (14927, Account name or description, null, 4, f, null, other, null, f, null, 1, null, 1, 2018-12-11 18:10:24.091826, 1, 2018-12-11 18:10:24.091826, t).
CONTEXT:  SQL statement "UPDATE ONLY "public"."account_account" SET "user_type_id" = NULL WHERE $1 OPERATOR(pg_catalog.=) "user_type_id""
2018-12-11 20:35:31,763 18018 WARNING db_name odoo.modules.loading: Transient module states were reset
2018-12-11 20:35:31,763 18018 ERROR db_name odoo.modules.registry: Failed to load registry
Traceback (most recent call last):
  File "/path/to/odoo/src/modules/registry.py", line 85, in new
    odoo.modules.load_modules(registry._db, force_demo, status, update_module)
  File "/path/to/odoo/src/modules/loading.py", line 414, in load_modules
    env['ir.model.data']._process_end(processed_modules)
  File "/path/to/odoo/src/linked/base/ir/ir_model.py", line 1628, in _process_end
    record.unlink()
  File "/path/to/odoo/src/models.py", line 2935, in unlink
    cr.execute(query, (sub_ids,))
  File "/path/to/odoo/src/sql_db.py", line 155, in wrapper
    return f(self, *args, **kwargs)
  File "/path/to/odoo/src/sql_db.py", line 232, in execute
    res = self._obj.execute(query, params)
psycopg2.IntegrityError: null value in column "user_type_id" violates not-null constraint
This is raised because the account type is being used by some account.

问题

  • 那么唯一的解决方案是使用现有的帐户类型而不是创建新的帐户类型?

  • 源代码中是否有执行此取消链接操作的位置?有人知道吗?

  • 这是正常行为吗?

注意:我是从头开始手动导入会计科目表

【问题讨论】:

    标签: python xml odoo odoo-11 odoo-accounting


    【解决方案1】:

    这应该是正常行为,尤其是对于 IIRC 根本不是特殊模型的 account.account.type。

    在模块更新时,odoo 总是删除不存在的外部 ID 的记录。在这种情况下,不存在的外部 ID 是什么?它们是系统中的外部 ID,例如您的 l10n_es.account_type_third_parties,它们不再在模块中(此处为 l10n_es)。

    实际上,这对于 odoo 开发人员来说是非常重要的信息。我不确定它是否在文档中。

    关于代码:它正在发生here

    @api.model
    def _process_end(self, modules):
        """ Clear records removed from updated module data.
        This method is called at the end of the module loading process.
        It is meant to removed records that are no longer present in the
        updated data. Such records are recognised as the one with an xml id
        and a module in ir_model_data and noupdate set to false, but not
        present in self.loads.
        """
        if not modules or tools.config.get('import_partial'):
            return True
    
        bad_imd_ids = []
        self = self.with_context({MODULE_UNINSTALL_FLAG: True})
    
        query = """ SELECT id, name, model, res_id, module FROM ir_model_data
                    WHERE module IN %s AND res_id IS NOT NULL AND noupdate=%s ORDER BY id DESC
                """
        self._cr.execute(query, (tuple(modules), False))
        for (id, name, model, res_id, module) in self._cr.fetchall():
            if (module, name) not in self.loads:
                if model in self.env:
                    _logger.info('Deleting %s@%s (%s.%s)', res_id, model, module, name)
                    record = self.env[model].browse(res_id)
                    if record.exists():
                        record.unlink()
                    else:
                        bad_imd_ids.append(id)
        if bad_imd_ids:
            self.browse(bad_imd_ids).unlink()
        self.loads.clear()
    

    【讨论】:

    • 就是这样!!我已将外部 id 的模块部分更改为__import__.account_type_third_parties,到目前为止问题消失了,模块不再试图删除它。很好的答案! :)
    • 好问题和好答案。但是,在 Odoo 10 中对同一模块 (l10n_es) 的代码执行 git pull 之后,我遇到了同样的问题。但是在更新所有记录时,该模块正在删除所有记录,即使是那些 XML ID 未更改的记录通过代码更新。所以到目前为止我找不到解决方案。我已经发布了这个问题:stackoverflow.com/questions/69841444/….
    猜你喜欢
    • 2016-10-16
    • 2022-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    相关资源
    最近更新 更多