openwisp/openwisp-users

[bug] Organization.add_user INTEGRITY error

Closed this issue · 2 comments

Try using the add_user method of the Organization model to add an user to an organization which does not have a user yet, eg:

org = Organization.objects.create(...)
user = User.objects.create(...)
org.add_user(user)

Expected result: works without errors.
Actual result:

django.db.utils.IntegrityError: UNIQUE constraint failed: openwisp_users_organizationowner.organization_user_id

We have a receiver in OpenWISP Users which automatically creates an organization owner if a user is added to an organization which doesn't have any owner. Apparently the add_user method tries to do that again which triggers this exception.

can you assign me this issue ?

Hi @nemesifier,
I've created a PR for this solution. Please have a look when possible.