In custom module if I select date range between 01-01-2015 to 23-12-2015 getting this error: (if range is 01-12-2015 to 23-12-2015 no problem at all)
Integrity Error
The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set
[object with reference: Product Template - product.template]
In cli:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/openerp/sql_db.py", line 234, in execute
res = self._obj.execute(query, params)
IntegrityError: insert or update on table "product_data" violates foreign key constraint "product_data_product_id_fkey"
DETAIL: Key (product_id)=(214) is not present in table "product_template".
code:
......
_columns = {
'product': fields.one2many('product.data','check_id', string="Product"),
'location_id': fields.many2one('stock.location','location'),
'st_date': fields.date('Start date'),
'en_date': fields.date('End date'),
'nod': fields.function(_days_between, type='integer', string='NoD'),
'now': fields.function(_weeks_between, type='integer', string='NoW'),
'nom': fields.function(_weeks_between, type='integer', string='NoM'),
}
def _get_def_loc(self, cr, uid, context=None):
dest_loc = self.pool.get('stock.location').search(cr, uid, [('usage', '=', 'production')], context=context)
dest_loc_obj = self.pool.get('stock.location').browse(cr, uid, dest_loc[0])
return dest_loc_obj.id
_defaults = {
'location_id': _get_def_loc,
}
def moves_get(self, cr, uid, ids, context=None):
a = 0
res = []
total = 0
lsrv = 0
hsrv = 0
asrv = 0
mwosres = 0
tot_qty = 0
product_id = 0
product_qty = 0
if context.has_key('prod_id'):
product_id = context.get('prod_id', 0)
for obj in self.browse(cr, uid, ids):
cr.execute(""" select product_id, sum(qty) from stock_quant where in_date ::date>=to_date('%s', 'YYYY-MM-DD') and
in_date ::date<= to_date('%s', 'YYYY-MM-DD') and location_id =%s group by product_id;""" %(obj.st_date,obj.en_date, obj.location_id.id))
tot_qty = cr.fetchall()
if product_id == True:
print 'Product ', product_id, ' removed'
else:
if tot_qty:
for rec in tot_qty:
total += int(rec[1])
lsrv = int(min(str,rec[1]))
hsrv = int(rec[1])
a = a + 1
asrv = int((total / a))
mwosres = int(rec[1] / obj.nod)
res.append((0, 0, {'product_id': rec[0],'product_qty':rec[1],'lsr':lsrv,'hsr':hsrv,'asr':asrv,'mwosres':mwosres}))
ab = obj.write({'product':res})
print '========================================================================'
print 'RESULT:', res
print 'MWoS: ', mwosres
print 'LSR: ', lsrv
print 'HSR: ', hsrv
print 'ASR: ', str(asrv) + ' p day'
print 'TOTAL: ', str(total) + ' sold in selected period'
print '========================================================================'
.......
If I select the same range in stock moves everything works fine ....
What can cause this problem ?
I can't find the culprit ....
2015-12-23 12:57:20,609 10641 INFO GNGnew werkzeug: 192.168.2.217 - - [23/Dec/2015 12:57:20] "POST /web/dataset/call_kw/stock.check/write HTTP/1.1" 200 -
2015-12-23 12:57:20,767 10641 ERROR GNGnew openerp.sql_db: bad query: INSERT INTO "product_data" ("id", "asr", "product_id", "check_id", "hsr", "mwosres", "lsr", "product_qty", "location_id", "create_uid", "write_uid", "create_date", "write_date") VALUES(nextval('product_data_id_seq'), '31', 214, 30, '1', 0, '1', 1, 7, 1, 1, (now() at time zone 'UTC'), (now() at time zone 'UTC')) RETURNING id
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/openerp/sql_db.py", line 234, in execute
res = self._obj.execute(query, params)
IntegrityError: insert or update on table "product_data" violates foreign key constraint "product_data_product_id_fkey"
DETAIL: Key (product_id)=(214) is not present in table "product_template".
2015-12-23 12:57:20,767 10641 ERROR GNGnew openerp.http: Exception during JSON request handling.
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 530, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 567, in dispatch
result = self._call_function(**self.params)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 303, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", line 149, in wrapper
raise openerp.osv.orm.except_orm(_('Integrity Error'), msg)
except_orm: ('Integrity Error', 'The operation cannot be completed, probably due to the following:\n- deletion: you may be trying to delete a record while other records still reference it\n- creation/update: a mandatory field is not correctly set\n\n[object with reference: Product Template - product.template]')