MouTio
MouTio

Reputation: 1289

Database table not deleted after uninstalling module in odoo 9

I have a custom module in odoo 9. In the model, I have animals and cages.

In a cage can be more than one animal. An animal can be only in one cage.

That is my model:

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"),
                }

When I install the module, everything works fine. But if I try to uninstall it, the table "Animal" remains in the database and there is an error in the console:

2016-10-31 16:37:21,091 28082 INFO myserver openerp.addons.base.ir.ir_model: Unable to delete [email protected]
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)

I have reviewed the official documentation, but I am not able to locate the error.

What is wrong in the model?

Upvotes: 1

Views: 1641

Answers (2)

ifixthat
ifixthat

Reputation: 6295

@MouTio,

I observed that you are using traditional v7 api in v9 and that is a big red flag as the old API had a lot of inconsistencies with Db relation. Rather I will advise you to migrate your module to the new API using the following guidelines:

  1. Porting from the old API to the new API
  2. Odoo new API guideline

So your migrated code may look like below:

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")

Also there is one important thing to notice here many2one can not be defined and loaded in memory/DB before the relation table has been created as many2one is hard FK in DB, while in inverse one2many field can be defined even before relation model has been loaded as one2many is not a hard relation in DB.

Please port your code to new API and then try uninstalling but note here, Odoo does not advise uninstalling module but unfortunately, they never wrote it in any documentation or made it publically known.

Upvotes: 1

Phillip Stack
Phillip Stack

Reputation: 3378

cage.passwords has a requirement for animal.cage_id you need to make allowances should the cage_id be deleted. You might also want to convert to models.Model not osv. I am not sure if you can provide an ondelete='set null' in an osv model.

Upvotes: 1

Related Questions