Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Why doesn't fetchall() return the same thing as the SQL request?

Yug Faa
on 2/14/13, 9:53 AM 3,453 views

When I run this query on Postgresql :

SELECT code, total FROM hr_payslip_line WHERE slip_id=1

I get :

|  code    |    total      |
|  BASE    |    57000.00   |

And when I run this code :

    sql = '''
        SELECT code, total FROM hr_payslip_line WHERE slip_id=%s
    ''' % (slip.id)
    for code, total in cr.fetchall() :                
        raise osv.except_osv(_('Info'),_('code : %s\nTotal :  %s\nSlip ID : %s\nSQL : %s ' % (code, total,slip.id,sql)))

I've used the raise to catch the error I get this dialog :


code : BASE
Total : None
Slip ID : 1
SQL : SELECT code, total FROM hr_payslip_line WHERE slip_id=1

The problem is that the total return None and not 57000.00 More informations : 1. I've one slip 2. I've one line 3. the field total is a stored function

Can you add slip.id to your debug message and post the result?

Daniel Reis
on 2/14/13, 10:23 AM

The debug is updated, I have used also res = cr.fetchall() and setting res[0] get 'BASE' and res[1] get False

Yug Faa
on 2/14/13, 10:36 AM
Quentin De Paoli (qdp)
On 2/14/13, 10:30 AM

that's because your code is wrong :-) you should have used

for row in cr.fetchall() :                
        raise osv.except_osv(_('Info'),_('code : %s\nTotal :  %s ' % (row[0], row[1])))

you can also use

    for row in cr.dictfetchall() :                
            raise osv.except_osv(_('Info'),_('code : %s\nTotal :  %s ' % (row['code'], row['total'])))

or you can (could?) also use the browse record instead of using an SQL query that ignore the orm (and the access rights checking for example): try with the following:

for line in slip.line_ids:
    raise osv.except_osv(_('Info'),_('code : %s\nTotal :  %s ' % (line.code, line.total)))

if, even with that you cannot get a value for line.total, it means that the total line is not yet computed. To force the computation, just manually call _calculate_total().

I replaced the code by the last one I get the same thing code='BASE' and Total=None,

Yug Faa
on 2/14/13, 10:40 AM

na... you should have done something wrong. Print the row before the raise please

Quentin De Paoli (qdp)
on 2/14/13, 10:52 AM

for row in cr.fetchall() : raise osv.except_osv(_('Info'),_('Row : %s' % (row,))) I get : Info

Row : (u'BASE', None) I guess that the problem is the type of total in the orm because it's a field calcultaed but it's stored !!

Yug Faa
on 2/14/13, 10:57 AM

i don't get the reason, this kind of code is widely used in OpenERP and without any problem... but you can still use the browse record to access the total -_- I updated my answer with a code avoiding the SQL query...

Quentin De Paoli (qdp)
on 2/14/13, 11:19 AM

It's my first thing that I have done, using browse get the same thing, when i print line.total I get None so I switch to SQL as solution but not work, it's a very complicate case, but if i find the problem I will publish it, I'm already advanced In OpenObject, and it's first time that I face som

Yug Faa
on 2/14/13, 11:24 AM

calling the function _calculate_total() has resolved the problem.

Yug Faa
on 2/15/13, 12:13 PM

Fabien Pinckaers (fp)

--Fabien Pinckaers (fp)--
| 6 8 8
fsdsdfsdf, Belgium
--Fabien Pinckaers (fp)--

Odoo Founder & CEO

Fabien Pinckaers (fp)
On 2/15/13, 3:43 AM

Your code is correct, there are two possibilities:

  • You changed the value of total before or after the code you shown. As you used raise, the transactions are rollbacked and, when you perform your manual query in SQL, you do not see the changed value. Do a cr.commit() just before raise, you may get NULL in your manual SQL query after calling the python code.
  • Both tests are not on the same database.


Quentin De Paoli (qdp)
on 2/15/13, 6:37 AM

Of course the moment when the query is executed the value of total is False, there is an interference, but how can I get the same thing as the sql request, I use just one database, and I puted cr.commit() before the sql execution, and always same thing, I can share with you all the file

Yug Faa
on 2/15/13, 7:57 AM
Yug Faa
On 2/15/13, 9:12 AM

I guess that the problem is an interference between the function compute_sheet in the hr_payroll module and my function

I have a stored fucntion field that get total from what shown above, but I put store=False it works well, for me it's not logic and I declared this as a bug,

There is somthing that not good in this module, as a advanced developper I get always values of stored function in the database using SQL and anawhere, this time it's different for me, If I Try to use cr.commit(), there isn't any changes, if I use cr.rollback() I get an other message that say that a field quantity from the object hr_payslip_line doesn't exist, for my module there is not any treatement of field quantity

So if you have a logic to resolve this problem, I'm gratfull for you

To recap : I want to compute a stored function field, I execute the code above and it don't work

just manually call _calculate_total()

Quentin De Paoli (qdp)
on 2/15/13, 10:50 AM

Ohh Quentin you're found a path , Thank you now I see 57000.00, I have to set this resolved

Yug Faa
on 2/15/13, 11:18 AM

ok let me add this in my answer, then you can check my answer as correct ^^

Quentin De Paoli (qdp)
on 2/15/13, 11:21 AM
On 2/14/13, 10:07 PM

cr.execute('''SELECT code, total FROM hr_payslip_line WHERE slip_id=%s''' % (slip.id)) x = cr.fetchone() #Only if it returns one (1) row

raise osv.except_osv('Info', 'code : %s\nTotal : %s' % (x[0], x[1]))

#or to return just one row SELECT code, SUM(total) FROM hr_payslip_line WHERE slip_id=1 GROUP BY code

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


Asked: 2/14/13, 9:53 AM
Seen: 3453 times
Last updated: 3/16/15, 8:10 AM