File "/usr/local/lib/python3.10/site-packages/click_odoo_contrib/update.py", line 299, in OdooEnvironmentWithUpdate
    _update_db(
  File "/usr/local/lib/python3.10/site-packages/click_odoo_contrib/update.py", line 270, in _update_db
    _update_db_nolock(
  File "/usr/local/lib/python3.10/site-packages/click_odoo_contrib/update.py", line 248, in _update_db_nolock
    odoo.modules.registry.Registry.new(database, update_module=True)
  File "/usr/local/lib/python3.10/site-packages/decorator.py", line 232, in fun
    return caller(func, *(extras + args), **kw)
  File "/odoo/src/odoo/tools/func.py", line 87, in locked
    return func(inst, *args, **kwargs)
  File "/odoo/src/odoo/modules/registry.py", line 114, in new
    odoo.modules.load_modules(registry, force_demo, status, update_module)
  File "/odoo/src/odoo/modules/loading.py", line 536, in load_modules
    env['ir.model.data']._process_end(processed_modules)
  File "/odoo/src/odoo/addons/base/models/ir_model.py", line 2579, in _process_end
    self._process_end_unlink_record(record)
  File "/odoo/src/odoo/addons/base/models/ir_model.py", line 2508, in _process_end_unlink_record
    record.unlink()
  File "/odoo/src/odoo/addons/base/models/res_users.py", line 1492, in unlink
    res = super(GroupsView, self).unlink()
  File "/odoo/src/odoo/models.py", line 4244, in unlink
    cr.execute(SQL(
  File "/odoo/src/odoo/sql_db.py", line 332, in execute
    res = self._obj.execute(query, params)
psycopg2.errors.ForeignKeyViolation: update or delete on table "res_groups" violates foreign key constraint "ir_model_access_group_id_fkey" on table "ir_model_access"
DETAIL:  Key (id)=(94) is still referenced from table "ir_model_access".

Error: update or delete on table "res_groups" violates foreign key constraint "ir_model_access_group_id_fkey" on table "ir_model_access"
DETAIL:  Key (id)=(94) is still referenced from table "ir_model_access".
from odoo import fields, models, api
from odoo.exceptions import AccessError


class Users(models.Model):
    _inherit = "res.users"

    position = fields.Char(string='Position')
    type = fields.Selection([
        ('admin', 'Admin'),
        ('moderator', 'Moderator'),
        ('reader', 'Reader'),
    ], string='User Type', required=True, default='reader')
    
    role = fields.Selection([
        ('republic', 'Republic'),
        ('region', 'Region'),
        ('district', 'District'),
        ('mahalla', 'Mahalla'),
    ], string='Role', default='mahalla')
    
    region_id = fields.Many2one('region', string="Region")
    district_id = fields.Many2one('district', string="District")
    mahalla_id = fields.Many2one('mahalla', string="Mahalla")

    @api.onchange('type')
    def _onchange_type(self):
        if self.type == 'admin':
            self.role = False
            self.region_id = False
            self.district_id = False
            self.mahalla_id = False
        self._update_role_visibility()
    
    @api.onchange('role')
    def _onchange_role(self):
        if self.type != 'admin':
            self._update_role_visibility()
    
    def _update_role_visibility(self):
        if self.type == 'admin':
            pass
        elif self.role == 'republic':
            pass
        elif self.role == 'region':
            self.district_id = False
            self.mahalla_id = False
        elif self.role == 'district':
            self.mahalla_id = False
        elif self.role == 'mahalla':
            pass

    def action_change_password(self):
        self.ensure_one()
        return {
            'type': 'ir.actions.act_window',
            'res_model': 'change.password.wizard',
            'view_mode': 'form',
            'view_id': self.env.ref('base.change_password_wizard_view').id,
            'target': 'new',
            'context': {'default_user_id': self.id},
        }

    def assign_group_based_on_role(self):
        self.ensure_one()

        admin_group = self.env.ref('mahallam.group_admin')
        group_republic = self.env.ref('mahallam.group_republic')
        group_region = self.env.ref('mahallam.group_region')
        group_district = self.env.ref('mahallam.group_district')
        group_mahalla = self.env.ref('mahallam.group_mahalla')
        group_mahalla = self.env.ref('mahallam.group_mahalla')
        group_reader = self.env.ref('mahallam.group_reader')
        group_moderator = self.env.ref('mahallam.group_moderator')
        group_hierarchical = self.env.ref('mahallam.group_hierarchical')
        group_localization = self.env.ref('mahallam.group_localization')
        group_application = self.env.ref('mahallam.group_application')
        group_announcement = self.env.ref('mahallam.group_announcement')
        group_notification = self.env.ref('mahallam.group_notification')
        group_questionnaire = self.env.ref('mahallam.group_questionnaire')
        group_user_questionnaire = self.env.ref('mahallam.group_user_questionnaire')
        group_system = self.env.ref('mahallam.group_system')
        group_user = self.env.ref('mahallam.group_user')

        self.groups_id = [
            (3, admin_group.id), (3, group_republic.id), (3, group_region.id),
            (3, group_district.id), (3, group_mahalla.id),
            (3, group_reader.id), (3, group_moderator.id),
            (3, group_hierarchical.id), (3, group_localization.id),
            (3, group_application.id), (3, group_announcement.id),
            (3, group_notification.id), (3, group_questionnaire.id),
            (3, group_user_questionnaire.id),
            ]

        if self.type == 'moderator':
            self.groups_id = [(4, group_moderator.id)]
        elif self.role == 'republic':
            self.groups_id = [(4, group_republic.id)]
        elif self.role == 'region':
            self.groups_id = [(4, group_region.id), (4, group_localization.id), (4, group_hierarchical.id)]
        elif self.role == 'district':
            self.groups_id = [(4, group_district.id), (4, group_localization.id), (4, group_hierarchical.id)]
        elif self.role == 'mahalla':
            self.groups_id = [(4, group_mahalla.id), (4, group_localization.id), (4, group_hierarchical.id)]
        elif self.type == 'reader':
            self.groups_id = [(4, group_reader.id)]

    @api.model
    def create(self, vals):
        user = super(Users, self).create(vals)
        user.assign_group_based_on_role()
        return user

    def write(self, vals):
        res = super(Users, self).write(vals)
        if 'type' in vals or 'role' in vals:
            self.assign_group_based_on_role()
        return res

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <data noupdate="1">
        
        <record id="mahallam.group_admin" model="res.groups">
            <field name="name">Group admin</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_republic" model="res.groups">
            <field name="name">Group republic</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_region" model="res.groups">
            <field name="name">Group region</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_district" model="res.groups">
            <field name="name">Group district</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_mahalla" model="res.groups">
            <field name="name">Group mahalla</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_reader" model="res.groups">
            <field name="name">Group reader</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_moderator" model="res.groups">
            <field name="name">Group moderator</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_hierarchical" model="res.groups">
            <field name="name">Group hierarchical</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_localization" model="res.groups">
            <field name="name">Group localization</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_application" model="res.groups">
            <field name="name">Group application</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_announcement" model="res.groups">
            <field name="name">Group announcement</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_notification" model="res.groups">
            <field name="name">Group notification</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_questionnaire" model="res.groups">
            <field name="name">Group questionnaire</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_user_questionnaire" model="res.groups">
            <field name="name">Group user questionnaire</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="base.group_system" model="res.groups">
            <field name="name">Group system</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="base.group_user" model="res.groups">
            <field name="name">Group user</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
    </data>
</odoo>