'Record the creation of a constraint for this model, to make it possible
to delete it later when the module is uninstalled. Type can be either
\'f\' or \'u\' depending on the constraint being a foreign key or not.'

@api.model_cr
def _save_constraint(self, constraint_name, type, definition, module):

    if (not module):
        return
    assert (type in ('f', 'u'))
    cr = self._cr
    cr.execute('\n                                                SELECT    type,    definition    FROM    ir_model_constraint,    ir_module_module\n                                                WHERE    ir_model_constraint.module=ir_module_module.id\n                                                                AND    ir_model_constraint.name=%s\n                                                                AND    ir_module_module.name=%s\n                                                ', (constraint_name, module))
    constraints = cr.dictfetchone()
    if (not constraints):
        cr.execute("\n                                                                INSERT    INTO    ir_model_constraint\n                                                                                (name,    date_init,    date_update,    module,    model,    type,    definition)\n                                                                VALUES    (%s,    now()    AT    TIME    ZONE    'UTC',    now()    AT    TIME    ZONE    'UTC',\n                                                                                (SELECT    id    FROM    ir_module_module    WHERE    name=%s),\n                                                                                (SELECT    id    FROM    ir_model    WHERE    model=%s),    %s,    %s)", (constraint_name, module, self._name, type, definition))
    elif ((constraints['type'] != type) or (definition and (constraints['definition'] != definition))):
        cr.execute("\n                                                                UPDATE    ir_model_constraint\n                                                                SET    date_update=now()    AT    TIME    ZONE    'UTC',    type=%s,    definition=%s\n                                                                WHERE    name=%s    AND    module    =    (SELECT    id    FROM    ir_module_module    WHERE    name=%s)", (type, definition, constraint_name, module))
