在 odoo 9 中卸载模块后数据库 table 未删除
Database table not deleted after uninstalling module in odoo 9
我在odoo 9中有一个自定义模块。在模型中,我有动物和笼子。
一个笼子里可以养不止一只动物。
一只动物只能关在一个笼子里。
这是我的模型:
class Animal(osv.osv):
_name = 'Animal'
_columns = {
'name': fields.char('Animal Name', size=2048),
'cage_id': fields.many2one('cage', required=True, ondelete='cascade', string="Cage"),
}
class Cage(osv.osv):
_name = 'Cage'
_columns = {
'name': fields.char('Cage Name', size=100),
'animals': fields.one2many('Animal', 'cage_id', string="Animals"),
}
当我安装模块时,一切正常。但是,如果我尝试卸载它,table "Animal" 仍保留在数据库中并且控制台中出现错误:
2016-10-31 16:37:21,091 28082 INFO myserver openerp.addons.base.ir.ir_model: Unable to delete 7565@ir.model.fields
Traceback (most recent call last):
File "/etc/odoo/server/openerp/addons/base/ir/ir_model.py", line 1269, in unlink_if_refcount
self.pool[model].unlink(cr, uid, [res_id], context=context)
File "/etc/odoo/server/openerp/api.py", line 250, in wrapper
return old_api(self, *args, **kwargs)
File "/etc/odoo/server/openerp/addons/base/ir/ir_model.py", line 461, in unlink
self.browse(cr, user, ids, context=context)._prepare_update()
File "/etc/odoo/server/openerp/api.py", line 248, in wrapper
return new_api(self, *args, **kwargs)
File "/etc/odoo/server/openerp/addons/base/ir/ir_model.py", line 449, in _prepare_update
raise UserError(msg % (field, model._field_inverses[field][0]))
UserError: (u"The field 'animal.cage_id' cannot be removed because the field 'cage.passwords' depends on it.", None)
我看了官方文档,但找不到错误。
模型有什么问题?
cage.passwords
对 animal.cage_id
有要求,如果 cage_id
被删除,您需要留出余地。您可能还想转换为 models.Model
而不是 osv
。我不确定你是否可以在 osv 模型中提供 ondelete='set null'。
@MouTio,
我观察到您在 v9 中使用传统的 v7 api,这是一个很大的危险信号,因为旧的 API 与 Db 关系有很多不一致之处。相反,我会建议您使用以下指南将您的模块迁移到新的 API:
因此您迁移后的代码可能如下所示:
from openerp import api, fields, models, _
class Cage(models.Model):
_name = 'cage'
name = fields.Char(string='Cage Name', size=100)
animals = fields.One2many(comodel_name='Animal', inverse_name='cage_id', string="Animals")
class Animal(models.Model):
_name = 'animal'
name = fields.Char(string='Animal Name', size=2048),
cage_id = fields.Many2one(comodel_name='cage', required=True, ondelete='cascade', string="Cage")
还有一件重要的事情需要注意,在关系 table 被创建之前,many2one
不能被定义和加载到 memory/DB 中,因为 many2one
很难FK
在数据库中,而在反向 one2many
字段中甚至可以在关系模型加载之前定义,因为 one2many
在数据库中不是硬关系。
请将您的代码移植到新的 API 然后尝试卸载,但请注意,Odoo 不建议卸载模块,但不幸的是,他们从未将其写入任何文档或公开。
我在odoo 9中有一个自定义模块。在模型中,我有动物和笼子。
一个笼子里可以养不止一只动物。 一只动物只能关在一个笼子里。
这是我的模型:
class Animal(osv.osv):
_name = 'Animal'
_columns = {
'name': fields.char('Animal Name', size=2048),
'cage_id': fields.many2one('cage', required=True, ondelete='cascade', string="Cage"),
}
class Cage(osv.osv):
_name = 'Cage'
_columns = {
'name': fields.char('Cage Name', size=100),
'animals': fields.one2many('Animal', 'cage_id', string="Animals"),
}
当我安装模块时,一切正常。但是,如果我尝试卸载它,table "Animal" 仍保留在数据库中并且控制台中出现错误:
2016-10-31 16:37:21,091 28082 INFO myserver openerp.addons.base.ir.ir_model: Unable to delete 7565@ir.model.fields
Traceback (most recent call last):
File "/etc/odoo/server/openerp/addons/base/ir/ir_model.py", line 1269, in unlink_if_refcount
self.pool[model].unlink(cr, uid, [res_id], context=context)
File "/etc/odoo/server/openerp/api.py", line 250, in wrapper
return old_api(self, *args, **kwargs)
File "/etc/odoo/server/openerp/addons/base/ir/ir_model.py", line 461, in unlink
self.browse(cr, user, ids, context=context)._prepare_update()
File "/etc/odoo/server/openerp/api.py", line 248, in wrapper
return new_api(self, *args, **kwargs)
File "/etc/odoo/server/openerp/addons/base/ir/ir_model.py", line 449, in _prepare_update
raise UserError(msg % (field, model._field_inverses[field][0]))
UserError: (u"The field 'animal.cage_id' cannot be removed because the field 'cage.passwords' depends on it.", None)
我看了官方文档,但找不到错误。
模型有什么问题?
cage.passwords
对 animal.cage_id
有要求,如果 cage_id
被删除,您需要留出余地。您可能还想转换为 models.Model
而不是 osv
。我不确定你是否可以在 osv 模型中提供 ondelete='set null'。
@MouTio,
我观察到您在 v9 中使用传统的 v7 api,这是一个很大的危险信号,因为旧的 API 与 Db 关系有很多不一致之处。相反,我会建议您使用以下指南将您的模块迁移到新的 API:
因此您迁移后的代码可能如下所示:
from openerp import api, fields, models, _
class Cage(models.Model):
_name = 'cage'
name = fields.Char(string='Cage Name', size=100)
animals = fields.One2many(comodel_name='Animal', inverse_name='cage_id', string="Animals")
class Animal(models.Model):
_name = 'animal'
name = fields.Char(string='Animal Name', size=2048),
cage_id = fields.Many2one(comodel_name='cage', required=True, ondelete='cascade', string="Cage")
还有一件重要的事情需要注意,在关系 table 被创建之前,many2one
不能被定义和加载到 memory/DB 中,因为 many2one
很难FK
在数据库中,而在反向 one2many
字段中甚至可以在关系模型加载之前定义,因为 one2many
在数据库中不是硬关系。
请将您的代码移植到新的 API 然后尝试卸载,但请注意,Odoo 不建议卸载模块,但不幸的是,他们从未将其写入任何文档或公开。