This question has been flagged
2 Replies
9042 Views

Can somebody point me to this section? I can't find it... If not, how do I get my models loaded when they all reference each other? Can I use _sql to add constraints to all tables from the last model to be loaded?

Avatar
Discard
Best Answer

Check the models you are updating, they have a SQL section in the model definition. You can check it in the source code

Avatar
Discard
Author

The source code is the only worthwhile documentation I've found so far. I am trying to find examples on usage of the _sql predefined field but cannot locate it in any sources.

Best Answer

Hello,

Not sure that adding SQL constrain is the right way to do. OpenERP deal with it's own constraints at the Controller layer (MCV architecture). If I correctly understand OpenERP doc I saw time's ago; model definitions, constraints, ... are "OpenERP" object which are stored in pgSQL but... many constraints are not simply "translated" as SQL constraints, theses are simply defined within the python code of OpenERP (ie: should not be empty, must be valid EAN13, ...).

Even if I understand "native SQL" could be easier/faster, OpenERP is not "just a pgSQL DB", this in an ERP and I feel more comfortable to use the XML RPC layer and left the controller do his job (ie: If I forget something he will tell me, and if some table should be updated, it will do it by itslef).

Here is my kind opinion. Hope this will help you to select the better/safer technical solution for your project.

If you want to dig a bit wihtin the OpenERP "model", you could have a look to the "settings/parameters/structure of the DB/model" and look at the ir.model and ir.model.fields. Here are all the models/fields definitions used by OpenERP (ie: you could filter on many2many field type).

Have a nice day. @++ Nicolas

Avatar
Discard
Author

Thanks Nicolas, I'm not a big fan of ORM and the OpenERP one is at times confounding to me due to it's entity identity handling. I prefer a little more control but keep resisting the urge to bypass ORM and cursor execute my own queries. Have a nice day too!

Author

I think I have chosen the safer solution now. It is also much faster than refactoring my code only to run into the same errors again and again. I have models V m2m C, C m2m D, C o2m N, D m2o E and they work fine except from the level of V where it all blows up. Uncaught exceptions within ORM module only, not my models. At most this is a query with 3 joins so now I am putting that query into a view and building model from the view. My class diagrams are valid but ORM just cannot handle it. It creates intersect tables with no constraints - bad referential integrity that....

@mike what do you mean by "putting that query into a view and building model from the view.." , can you be more specific?

Author

Instead of using a table in postgres use a view - http://www.postgresql.org/docs/8.3/static/sql-createview.html and then build your model against that view as though it were a table. The model must have _auto = False to prevent OpenERP from creating a new table.