Odoo 多列索引

Odoo multicolumn index

我需要创建一个两列索引。我已经声明:

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

但这会创建两个独立的索引。我想获得一个综合指数。知道我怎样才能做到这一点吗?

必须直接在数据库中完成。 Odoo只支持单列自动创建索引

示例来自 PostgreSQL 9.6 Documentation

CREATE INDEX test2_mm_idx ON test2 (major, minor);

编辑:在 a talk which is documentated here Odoo 推荐使用模型的 init() 方法。

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)
        """)