Odoo Help

Welcome!

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.

1

Integrity Error on object creation

By
Yulybaseball
on 12/15/14, 10:51 AM 1,446 views

Hi there:

I am developing a new model called implementation.project; it inherits from project.project. This is the class declaration:

class implementation_project(osv.Model):
    _name = 'implementation.project'
    _inherit = 'project.project'

Some fields (columns) are added to this class (model or whatever). Original model (project.project) has a one2many field called 'tasks'. When I try to create new 'implementation.project' record, the enigmatic Odoo raises this error:

IntegrityError: insert or update on table "project_task_type_rel" violates foreign key constraint "project_task_type_rel_project_id_fkey"
DETAIL:  Key (project_id)=(6) is not present in table "project_project".

Obviously!!!! It has to exist project.project record to create anything that inherits from him!!! I guess Odoo (according to some, a good platform to develop) has the ability to know how to proceed in this case... or not????

So, how to create new record from 'implementation.project'????

 

Thanks in advance.

 

2
Yulybaseball
On 12/15/14, 4:40 PM

Note: I am sorry for posting an answer. It should be a Comment, but I can't: surprinsigly, I need 50 karma to do that... even for an answer to my question.

@Bole, first of all, THANK YOU VERY MUCH!!! It works!!!

Second, what you explained is entirely logical... but I assumed Odoo would better understand the inheritance. Why I have to assume I have to "redeclare" parent fields??? Please, it's an inheritance!!! If I have a model, and I have to inherit from it... what is the sense of the inheritance if I have to redeclare all fields??? (Assuming all parent fields are relations to other models).

Third, OK, I understand developers must know how works the platform they are going to develop on... but how to know how Odoo works? Where is the developer documentation?  

Fourth, I am sorry. I went to mark your reply as the Accepted one, but I need more Karma... 

 

 

correction.. you do not REDECLARE... you OVERRIDE inherited field in order to change (correct) the relation ... as for documentation... we (some developers) have a wery good friend ... his name is google.... try asking our mutual firend does he know something about "openerp technical memento" ... also... check http://doc.openerp.com for v7 or https://www.odoo.com/documentation/8.0/ for v8 ... after that.. you affocurse have this forum... and a lot of private blogistes .... so .. resources are all over.. all you need is time to read it and more time to understand it :)

Bole
on 12/16/14, 4:01 AM
1

Bole

--Bole--
2385
| 5 4 6
Zagreb | Karlovac, Croatia
--Bole--

there is only 10 kind of people the ones that understand binary ... and others

Bole
On 12/15/14, 12:12 PM

Well... 
class implementation_project(osv.Model):
    _name = 'implementation.project'
    _inherit = 'project.project'

What you did is, you created a completley new object 'implementation.project' wich inherited all fields from original class...
and then you added some new custom fields...
But, what you did NOT do is.. change some of inherited fields... one of them is :
'type_ids': fields.many2many('project.task.type', 'project_task_type_rel', 'project_id', 'type_id', 'Tasks Stages', states={'close':[('readonly',True)], 'cancelled':[('readonly',True)]}),

SO now you have your new model, with task type related to old model.. 
what you should do is override that field like:
'type_ids': fields.many2many('project.task.type', 'implementation_project_task_type_rel', 'project_id', 'type_id', 'Tasks Stages', states={'close':[('readonly',True)], 'cancelled':[('readonly',True)]}), 
in order to establish a new relatin table between your new created model and old task_type model... 

Now cretaing record should work just fine.. 

hope it helps : )

p.s. 
odoo is nice platform for rapid development of businees apps... but, in order to use it you have to firs know how it works...
in other words RTFM :)

 

0

Ivan

--Ivan--
3210
| 5 3 6
Jakarta, Indonesia
--Ivan--
Ivan
On 12/15/14, 6:08 PM

Odoo has a couple of way that you can inherit an object from.  One way is how you did, which is using the _inherit attribute with a string value that matches the name of the model that you want to inherit.  This way Odoo will extend the original model.  The class name bears no "meaning" to the ORM model.  The _name attribute, however may change the _name attribute of the original model (can't say for sure as I haven't really check it out).  When you create a record, you need to specify all the values of the necessary fields.  All values will be written on the same table.

The second way is to use _inherits = {'model.name': 'field_name'}.  This is done, e.g. for product.product in which it inherit product.template (check out the odoo/odoo/addons/product/product.py file).  This is a creation of another class that inherits a different parent class.  The sub class will have 'field_name' but all the fields/columns that belongs to the parent class will be written in the parent's class table.  When you create a record, you need to specify all the values of the necessary fields.  Values belonging to parent class will be written into the parent's table and values beloging to child class will be written in the child's table.

The third way, usually used for mixin is to use _inherit = ['mixin.model'].  You can also checkout product.product as it also inherits 'mail.thread'.  As it is just extension of capability, it adds no additional fields.

 

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

2 follower(s)

Stats

Asked: 12/15/14, 10:51 AM
Seen: 1446 times
Last updated: 3/16/15, 8:10 AM