0
votes

I'm trying to insert a new record in stock.quant. This works when I tried it via this SQL query:

INSERT INTO stock_quant (create_date, qty, create_uid, location_id, company_id, write_date, write_uid, product_id, in_date) VALUES (now(), +20, 1, 12, 1, now(), 1, 8, now())

Now I want to do the same thing in Odoo. I've tried it like this:

stock_quant_obj = self.env[('stock.quant')]
stock_quant = stock_quant_obj.search_read(['&', ('product_id', '=', id_huidigproduct), ('realtimemeting', '=', True)], ['qty']) 
if stock_quant == []:
    stock_quant_obj.create({'product_id': self.id, 'qty': 100, 'location_id:': 12, 'company_id': 1, 'realtimemeting': True})

But this gives me an integrity error:

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: location_id - location.id]

I think it might have something to do with location_id being a many2one field in stock.quant. But so is product_id which doesn't give errors.

I also tried to replace "12" with obj_magazijn and obj_magazijn.id:

obj_magazijn ==> stock.location(12,)
obj_magazijn.id ==> 12

and

obj_magazijn = self.env[('stock.location')].search([('id', '=', 12)])

Does anyone know the real reason for this error and/or know a solution for this?

1

1 Answers

1
votes

I don't think so there is anything wrong except syntax.

self.env[('stock.quant')] replaced with self.env['stock.quant']

learn more about Environment

stock_quant_obj = self.env['stock.quant']
#### here id_huidigproduct is unknown for me.
stock_quant = stock_quant_obj.search_read([('product_id', '=', id_huidigproduct), ('realtimemeting', '=', True)], ['qty']) 
if not stock_quant :
    stock_quant_obj.create({'product_id': self.id, 'qty': 100, 'location_id:': 12, 'company_id': 1, 'realtimemeting': True})