2015-09-28 66 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

你能否提供簡單的代碼細節。 –