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.


How to resolve "IndexError: tuple index out of range" in sql statement inside python?

Anirudh Lou
on 8/19/15, 9:20 PM 2,378 views

Following is my code chunks that aims to get the sum of fields/columns from a certain table:

atc_code = nature = ''
_logger.info("\n\t\t\tI was called. My method name is %s"%(str('generate_student_resource_report')))
base = amount = percent = 0.00
for ewt in self.browse(cr,uid,ids,context):
old_history = stud_re_report_line_obj.search(cr, uid, [('parent_id','=',ewt.id)],context=context)
if old_history:
stud_re_report_line_obj.unlink(cr, uid, old_history,context=context)
_logger.info("\n\t\t\tDATE FROM %s\n\t\t\tDATE TO %s"%(str(date_from),str(date_to)))
cr.execute("insert into student_resource_report_line(partner_id,seq,base_amt,\
select es.partner_id as partner_id,\
(case when es.name like '%WC158%' then 1\
when es.name like '%WC160%' then 2\
when es.name like '%WC010%' then 3\
when es.name like '%WC140%' then 4\
else 0 end) as seq,\
sum(es.base_amount) as base_amt,\
sum(es.tax_amount) as tax_amt,\
(case when es.name like '%EWT 1%%' then '1.00'\
when es.name like '%EWT 2%%' then '2.00'\
when es.name like '%EWT 3%%' then '3.00'\
when es.name like '%EWT 4%%' then '4.00'\
when es.name like '%EWT 5%%' then '5.00'\
when es.name like '%EWT 6%%' then '6.00'\
when es.name like '%EWT 7%%' then '7.00'\
when es.name like '%EWT 8%%' then '8.00'\
when es.name like '%EWT 9%%' then '9.00'\
when es.name like '%EWT 10%%' then '10.00'\
else null end) as percen,\
(case when es.name like '%WC158%' then 'WC158'\
when es.name like '%WC160%' then 'WC160'\
when es.name like '%WC010%' then 'WC010'\
when es.name like '%WC140%' then 'WC140'\
else null end) as atc_code,\
(case when es.name like '%WC158%' then 'NOTEBOOK'\
when es.name like '%WC160%' then 'BACKPACK'\
when es.name like '%WC010%' then 'COLOR'\
when es.name like '%WC140%' then 'BOOKS' else null end) as nature,\
(now()) as create_date,(now()) as write_date,? as parent_id\
from student_resource es where es.date between ? and ? \
group by es.partner_id,es.name",(ewt.id,ewt.date_from,ewt.date_to))
line_list = cr.fetchall()
_logger.info("\n\t\t\tSource ... %s"%(str(line_list)))
list = stud_re_report_line_obj.search(cr,uid,[('parent_id','=',ewt.id)])
lines = [line.id if line.id else False for line in stud_re_report_line_obj.browse(cr,uid,list,context=context)]
value = {
"value" : {
'name' : 'Student Resource Report',
"ewt_line" : lines
return value

My problem now is that when this method is called, It produce an error. It says:

  File "/opt/openerp/server-7/openerp/sql_db.py", line 226, in execute
res = self._obj.execute(query, params)
IndexError: tuple index out of range

and it also says that my query is bad, I dont know why? Everytime i tried it directly into PgAdmin, it works fine:

2015-08-20 01:00:10,780 5697 ERROR sample_another openerp.sql_db: bad query: insert into student_resource_report_line(partner_id,seq,base_amt, tax_at,percen,atc_code, ...

Any help/suggestion/comments is highly appreciated



| 4 3 5
Hyderabad, India

On 8/21/15, 11:21 AM


Sql Insert command's syntax is:
"insert into [your_table_name](optional_your_fields) values([values_to enter in the fields])"

Here 'insert' and 'values' are the keywords and must be there, but i can t see and "values" keyword the in your statement.

Please check out your statement...

Hope it helps you....    

Thank you for the reply. But as you see I am copying data from another table so I think it is not necessary to put "values" since its values will be equal to my select statement provided it meet its parameter in "insert into" statement.

Anirudh Lou
on 8/22/15, 4:04 AM

ops.... i missed that, ..... thanks for it...can u pls provide your error in detail....

on 8/24/15, 1:58 AM

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: 8/19/15, 9:20 PM
Seen: 2378 times
Last updated: 8/21/15, 11:21 AM