diff --git a/authentik/sources/ldap/sync/groups.py b/authentik/sources/ldap/sync/groups.py index 8cc671e45842..c714883d02be 100644 --- a/authentik/sources/ldap/sync/groups.py +++ b/authentik/sources/ldap/sync/groups.py @@ -97,12 +97,11 @@ def sync(self, page_data: list) -> int: if parent: ak_group.parents.add(parent) self._logger.debug("Created group with attributes", **defaults) - if not GroupLDAPSourceConnection.objects.filter( - source=self._source, identifier=uniq - ): - GroupLDAPSourceConnection.objects.create( - source=self._source, group=ak_group, identifier=uniq - ) + GroupLDAPSourceConnection.objects.update_or_create( + source=self._source, + group=ak_group, + defaults={"identifier": uniq}, + ) except SkipObjectException: continue except PropertyMappingExpressionException as exc: diff --git a/authentik/sources/ldap/sync/users.py b/authentik/sources/ldap/sync/users.py index a23e456f1a9c..4d52b04ab938 100644 --- a/authentik/sources/ldap/sync/users.py +++ b/authentik/sources/ldap/sync/users.py @@ -89,12 +89,11 @@ def sync(self, page_data: list) -> int: ak_user, created = User.update_or_create_attributes( {f"attributes__{LDAP_UNIQUENESS}": uniq}, defaults ) - if not UserLDAPSourceConnection.objects.filter( - source=self._source, identifier=uniq - ): - UserLDAPSourceConnection.objects.create( - source=self._source, user=ak_user, identifier=uniq - ) + UserLDAPSourceConnection.objects.update_or_create( + source=self._source, + user=ak_user, + defaults={"identifier": uniq}, + ) except PropertyMappingExpressionException as exc: raise StopSync(exc, None, exc.mapping) from exc except SkipObjectException: