There's more...

The Many2one fields support an additional auto_join attribute. It is a flag that allows the ORM to use SQL joins on this field. Due to this, it bypasses the usual ORM control such as user access control and record access rules. On a specific case, it can solve a performance issue, but it is advised to avoid using it.

We have seen the shortest way to define the relational fields. For completeness, these are the attributes specific to this type of field:

The One2many field attributes are as follows:

  • comodel_name: This is the target model identifier and is mandatory for all relational fields, but it can be defined position-wise without the keyword
  • inverse_name: This applies only to One2many and is the field name in the target model for the inverse Many2one relation
  • limit: This applies to One2many and Many2many and sets an optional limit on the number of records to read that are used at the user interface level

The Many2many field attributes are as follows:

  • comodel_name: (as defined earlier)
  • relation: This is the name to use for the table supporting the relation, overriding the automatically defined name
  • column1: This is the name for the Many2one field in the relational table linking to this model
  • column2: This is the name for the Many2one field in the relational table linking to comodel

For Many2many relations, in most cases, the ORM will take perfect care of the default values for these attributes. It is even capable of detecting inverse Many2many relations, detecting the already existing relation table, and appropriately inverting the column1 and column2 values.

However, there are two cases where we need to step in and provide our own values for these attributes. One is the case where we need more than one Many2many relation between the same two models. For this to be possible, we must provide ourselves the relation table name for the second relation, which must be different from the first relation. The other case is when the database names of the related tables are long enough for the automatically generated relation name to exceed the 63 character PostgreSQL limit for database object names.

The relation table's automatic name is <model1>_<model2>_rel. However, this relation table also creates an index for its primary key with the following identifier:

<model1>_<model2>_rel_<model1>_id_<model2>_id_key 

This primary key also needs to meet the 63 characters limit. So, if the two table names combined exceed a total of 63 characters, you will probably have trouble meeting the limits and will need to manually set the relation attribute.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.33.157