# Buggy code
def write(self, vals):
    res = super(OpStudent, self).write(vals)
    if vals.get("parent_ids", False):
        user_ids = []
        if self.parent_ids:
            for parent in self.parent_ids:
                if parent.user_id:
                    user_ids = [x.user_id.id for x in parent.student_ids if x.user_id]
                    parent.user_id.child_ids = [(6, 0, user_ids)]
        else:
            user_ids = self.env["res.users"].search(
                [("child_ids", "in", self.user_id.id)]
            )
            for user_id in user_ids:
                child_ids = user_id.child_ids.ids
                child_ids.remove(self.user_id.id)
                user_id.child_ids = [(6, 0, child_ids)]
    if vals.get("user_id", False):
        for parent_id in self.parent_ids:
            child_ids = parent_id.user_id.child_ids.ids
            child_ids.append(vals["user_id"])
            parent_id.name.user_id.child_ids = [(6, 0, child_ids)]
    self.clear_caches()
    return res


# Traceback (ValueError)
Traceback (most recent call last):

File "/media/linux_data/progetti/odoo/odoo12-dev/OCB/odoo/models.py", line 4718, in ensure_one

_id, = self._ids

ValueError: not enough values to unpack (expected 1, got 0)



During handling of the above exception, another exception occurred:



Traceback (most recent call last):

File "/media/linux_data/progetti/odoo/odoo12-dev/OCB/odoo/http.py", line 656, in _handle_exception

return super(JsonRequest, self)._handle_exception(exception)

File "/media/linux_data/progetti/odoo/odoo12-dev/OCB/odoo/http.py", line 314, in _handle_exception

raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])

File "/media/linux_data/progetti/odoo/odoo12-dev/OCB/odoo/tools/pycompat.py", line 87, in reraise

raise value

File "/media/linux_data/progetti/odoo/odoo12-dev/OCB/odoo/http.py", line 698, in dispatch

result = self._call_function(**self.params)

File "/media/linux_data/progetti/odoo/odoo12-dev/OCB/odoo/http.py", line 346, in _call_function

return checked_call(self.db, *args, **kwargs)

File "/media/linux_data/progetti/odoo/odoo12-dev/OCB/odoo/service/model.py", line 97, in wrapper

return f(dbname, *args, **kwargs)

File "/media/linux_data/progetti/odoo/odoo12-dev/OCB/odoo/http.py", line 339, in checked_call

result = self.endpoint(*a, **kw)

File "/media/linux_data/progetti/odoo/odoo12-dev/OCB/odoo/http.py", line 941, in __call__

return self.method(*args, **kw)

File "/media/linux_data/progetti/odoo/odoo12-dev/OCB/odoo/http.py", line 519, in response_wrap

response = f(*args, **kw)

File "/media/linux_data/progetti/odoo/odoo12-dev/OCB/addons/web/controllers/main.py", line 966, in call_button

action = self._call_kw(model, method, args, {})

File "/media/linux_data/progetti/odoo/odoo12-dev/OCB/addons/web/controllers/main.py", line 954, in _call_kw

return call_kw(request.env[model], method, args, kwargs)

File "/media/linux_data/progetti/odoo/odoo12-dev/OCB/odoo/api.py", line 749, in call_kw

return _call_kw_multi(method, model, args, kwargs)

File "/media/linux_data/progetti/odoo/odoo12-dev/OCB/odoo/api.py", line 736, in _call_kw_multi

result = method(recs, *args, **kwargs)

File "/media/linux_data/progetti/odoo/odoo12-dev/openeducat/openeducat_core/models/student.py", line 118, in create_student_user

record.user_id = user_id

File "/media/linux_data/progetti/odoo/odoo12-dev/OCB/odoo/fields.py", line 1024, in __set__

record.write({self.name: write_value})

File "/media/linux_data/progetti/odoo/odoo12-dev/openeducat/openeducat_parent/models/parent.py", line 134, in write

parent_id.name.user_id.child_ids = [(6, 0, child_ids)]

File "/media/linux_data/progetti/odoo/odoo12-dev/OCB/odoo/fields.py", line 1000, in __set__

record.ensure_one()

File "/media/linux_data/progetti/odoo/odoo12-dev/OCB/odoo/models.py", line 4721, in ensure_one

raise ValueError("Expected singleton: %s" % self)

ValueError: Expected singleton: res.users()

# Fixed code
def write(self, vals):
    res = super(OpStudent, self).write(vals)
    if vals.get("parent_ids", False):
        user_ids = []
        if self.parent_ids:
            for parent in self.parent_ids:
                if parent.user_id:
                    user_ids = [x.user_id.id for x in parent.student_ids if x.user_id]
                    parent.user_id.child_ids = [(6, 0, user_ids)]
        else:
            user_ids = self.env["res.users"].search(
                [("child_ids", "in", self.user_id.id)]
            )
            for user_id in user_ids:
                child_ids = user_id.child_ids.ids
                child_ids.remove(self.user_id.id)
                user_id.child_ids = [(6, 0, child_ids)]
    if vals.get("user_id", False):
        for parent_id in self.parent_ids:
            if parent_id.user_id:
                child_ids = parent_id.user_id.child_ids.ids
                child_ids.append(vals["user_id"])
                parent_id.name.user_id.child_ids = [(6, 0, child_ids)]
    self.clear_caches()
    return res