2015-09-28 41 views
0

我试图在stock.quant中插入新记录。这工作时,我通过这个SQL查询试了一下:Odoo:创建新记录时出现完整性错误

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()) 

现在我想做同样的事情在Odoo。 我已经试过这样:

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}) 

但是这给了我一个完整的错误:

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]

我想可能有一些做与LOCATION_ID是在stock.quant一个many2one场。但是product_id不会产生错误。

我也试图取代“12”与obj_magazijn和obj_magazijn.id:

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

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

有谁知道这个错误的真正原因和/或专有的解决方案这个?

回答

1

我不这么认为除了语法之外还有什么不对。

self.env [( 'stock.quant')]替换为self.env [ 'stock.quant']

了解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}) 
+0

仍然不工作... – RobbeM

+0

你能否提供简单的代码细节。 –