4

I need to create a two-column index. I have declared:

field_A= fields.Float(string='A', index=True) 
field_B= fields.Float(string='B', index=True)

But that creates two independent indices. I would like to obtain a composite index. Any idea how I could achieve this?

Dayana
  • 1,500
  • 1
  • 16
  • 29

1 Answers1

6

It has to be done directly in the database. Odoo only supports automatic index creation for single columns.

An example from PostgreSQL 9.6 Documentation

CREATE INDEX test2_mm_idx ON test2 (major, minor);

Edit: On a talk which is documentated here Odoo is recommending the use of the init() method of a model.

You maybe need (in rare case, PostgreSQL is able to combine indices) to add custom type of index or index on more than one field at the same time -> Use the init method to declare them

#Example on mail.message, index on two fields
@api.model_cr
def init(self):
    self._cr.execute("""
        SELECT indexname FROM pg_indexes 
        WHERE indexname = 'mail_message_model_res_id_idx'
    """)
    if not self._cr.fetchone():
        self._cr.execute("""
            CREATE INDEX mail_message_model_res_id_idx 
            ON mail_message (model, res_id)
        """)
CZoellner
  • 13,553
  • 3
  • 25
  • 38