Using non-char _rec_name columns fails with unclear traceback (should provide better feedback)

Bug #611059 reported by Martin Collins
16
This bug affects 2 people
Affects Status Importance Assigned to Milestone
Odoo Server (MOVED TO GITHUB)
Confirmed
Wishlist
OpenERP's Framework R&D

Bug Description

I have an object with:

_name = 'citrus.truck.load'
_rec_name = 'load_id'

_columns = { 'load_id: fields.integer('Load ID'),
etc...
}

And another object with:

 _columns = {
        'load_id': fields.many2one('citrus.truck.load', 'Load ID', select=True, required=True),
etc...
}

When I search for a Load ID in the second object by clicking the binoculars I get the list of citrus.truck.loads. That's good. But when I enter a full or part ID and hit tab I get a backtrace:

File "/usr/lib/openerp/server/bin/netsvc.py", line 401, in dispatch
    result = ExportService.getService(service_name).dispatch(method, auth, params)
  File "/usr/lib/openerp/server/bin/service/web_services.py", line 584, in dispatch
    res = fn(db, uid, *params)
  File "/usr/lib/openerp/server/bin/addons/base_module_record/base_module_record.py", line 43, in execute
    res = super(recording_objects_proxy, self).execute(*args, **argv)
  File "/usr/lib/openerp/server/bin/osv/osv.py", line 57, in wrapper
    return f(self, dbname, *args, **kwargs)
  File "/usr/lib/openerp/server/bin/osv/osv.py", line 133, in execute
    res = pool.execute_cr(cr, uid, obj, method, *args, **kw)
  File "/usr/lib/openerp/server/bin/osv/osv.py", line 123, in execute_cr
    return getattr(object, method)(cr, uid, *args, **kw)
  File "/usr/lib/openerp/server/bin/osv/orm.py", line 3871, in name_search
    return self._name_search(cr, user, name, args, operator, context, limit)
  File "/usr/lib/openerp/server/bin/osv/orm.py", line 3852, in _name_search
    ids = self.search(cr, user, args, limit=limit, context=context)
  File "/usr/lib/openerp/server/bin/osv/orm.py", line 3807, in search
    cr.execute('select %s.id from ' % self._table + ','.join(tables) +qu1+' order by '+order_by+limit_str+offset_str, qu2)
  File "/usr/lib/openerp/server/bin/sql_db.py", line 74, in wrapper
    return f(self, *args, **kwargs)
  File "/usr/lib/openerp/server/bin/sql_db.py", line 119, in execute
    res = self._obj.execute(query, params)
ProgrammingError: operator does not exist: integer ~~* unknown
LINE 1: ...trus_truck_load" where (citrus_truck_load.load_id ilike E'%0...
                                                             ^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

OpenERP assumes the target field is char and tries to do a search using ILIKE which doesn't work with integer fields like this.

Changed in openobject-server:
status: New → Confirmed
Revision history for this message
JMA(Open ERP) (jma-openerp) wrote :

Hi Martin,

We confirm the error.
Generally, the standard says that the _rec_name field should always be of type char(string).
This field(if referenced as m2o) is used to represent the record.

If the field is not supposed to be of type char, one should override the name_get() method and return a string that can serve the aim.

There is a fix available, but it is possible that we have to cover all non-char type fields,operators as well.

Hope this helps.
Thanks.

Changed in openobject-server:
status: Confirmed → New
Revision history for this message
Albert Cervera i Areny - http://www.NaN-tic.com (albert-nan) wrote : Re: [Bug 611059] Re: Many2one search doesn't work with integer fields.

A Dilluns, 30 d'agost de 2010, JMA(Open ERP) va escriure:
> Hi Martin,
>
> We confirm the error.
> Generally, the standard says that the _rec_name field should always be of
> type char(string). This field(if referenced as m2o) is used to represent
> the record.
>
> If the field is not supposed to be of type char, one should override the
> name_get() method and return a string that can serve the aim.
>
> There is a fix available, but it is possible that we have to cover all
> non-char type fields,operators as well.

Why not throwing an exception when starting the server if _rec_name points to
a non-char field?

>
> Hope this helps.
> Thanks.
>
> ** Changed in: openobject-server
> Status: Confirmed => New

--
Albert Cervera i Areny
http://www.NaN-tic.com
OpenERP Partners
Mòbil: +34 669 40 40 18

http://twitter.com/albertnan
http://albert-nan.blogspot.com

Revision history for this message
Samantha (samantha-z-mathews) wrote : Re: Many2one search doesn't work with integer fields.

Sorry,but I strongly disagree with Jay(OpenERP) and Albert Cerver.
One should put char field on definition of a table.

Though I am not expert in python, I agree to JMA(OpenERP) who explained things nicely to deal with this scenario.
if i m not wrong, there are many examples for modules who play with name_get methods.

my 2 cents,
thanks.

Changed in openobject-server:
status: New → Invalid
Revision history for this message
Albert Cervera i Areny - http://www.NaN-tic.com (albert-nan) wrote :

Samantha, my proposal is to force the field to be string, but instead of waiting for the application to crash when the user does a given action, be a little bit smarter and throw an exception on server start so developer will be aware of the mistake sooner.

Revision history for this message
Jay Vora (Serpent Consulting Services) (jayvora) wrote :

Hello Samantha,

Albert has a good point. The approach is not to stay away from errors, but to let the developer know what mistake he has done.
Thus, providing a cleaner architecture.

I am re-opening the bug, suggestions are invited.
Thanks.

Changed in openobject-server:
status: Invalid → Confirmed
importance: Undecided → Wishlist
summary: - Many2one search doesn't work with integer fields.
+ Using non-char _rec_name columns fails with obscure traceback
summary: - Using non-char _rec_name columns fails with obscure traceback
+ Using non-char _rec_name columns fails with unclear traceback (should
+ provide better feedback)
Revision history for this message
Olivier Dony (Odoo) (odo-openerp) wrote :

Hello Martin,

This is a limitation of the current framework, the _rec_name field should be a field type that can be searched with a string expression, such as a char or many2one field.

If you want to use another type, you need to simply override the name_search() method to support this behavior the way you want it.

The server should however provide better feedback when this error happens, as suggested by some comments, which is why we confirm this bug as wishlist.

Thanks for reporting!

Changed in openobject-server:
assignee: nobody → OpenERP's Framework R&D (openerp-dev-framework)
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.