from odoo import models, fields


class EncoachUserEntityRel(models.Model):
    _name = "encoach.user.entity.rel"
    _description = "EnCoach User-Entity Membership"

    user_id = fields.Many2one("res.users", required=True, ondelete="cascade")
    entity_id = fields.Many2one("encoach.entity", required=True, ondelete="cascade")
    role_id = fields.Many2one("encoach.role", ondelete="set null")

    _user_entity_unique = models.Constraint(
        "UNIQUE(user_id, entity_id)",
        "A user can only have one membership per entity.",
    )
