Skip to content

Add non-empty constraint for UserSocialAuth uid #557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 5.1.7 on 2025-03-14 12:16

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("social_django", "0016_alter_usersocialauth_extra_data"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.AddConstraint(
model_name="usersocialauth",
constraint=models.CheckConstraint(
check=models.Q(("uid", ""), _negated=True),
name="user_social_auth_uid_required",
),
Comment on lines +16 to +19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same django.VERSION approach can be used here

Suggested change
constraint=models.CheckConstraint(
check=models.Q(("uid", ""), _negated=True),
name="user_social_auth_uid_required",
),
constraint=(
models.CheckConstraint(condition=~models.Q(uid=""), name="user_social_auth_uid_required")
if django.VERSION >= (5, 1)
else models.CheckConstraint(check=~models.Q(uid=""), name="user_social_auth_uid_required")
),

),
]
7 changes: 6 additions & 1 deletion social_django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import Union

import django
from django.conf import settings
from django.db import models
from django.db.utils import IntegrityError
Expand Down Expand Up @@ -43,6 +44,10 @@

class Meta:
app_label = "social_django"
if django.VERSION[0] < 6:
constraints = [models.CheckConstraint(check=~models.Q(uid=""), name="user_social_auth_uid_required")]
else:
constraints = [models.CheckConstraint(condition=~models.Q(uid=""), name="user_social_auth_uid_required")]

Check warning on line 50 in social_django/models.py

View check run for this annotation

Codecov / codecov/patch

social_django/models.py#L50

Added line #L50 was not covered by tests
Comment on lines +47 to +50

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to favor the non-deprecated way instead

Suggested change
if django.VERSION[0] < 6:
constraints = [models.CheckConstraint(check=~models.Q(uid=""), name="user_social_auth_uid_required")]
else:
constraints = [models.CheckConstraint(condition=~models.Q(uid=""), name="user_social_auth_uid_required")]
if django.VERSION >= (5, 1):
constraints = [models.CheckConstraint(condition=~models.Q(uid=""), name="user_social_auth_uid_required")]
else:
constraints = [models.CheckConstraint(check=~models.Q(uid=""), name="user_social_auth_uid_required")]

abstract = True

@classmethod
Expand Down Expand Up @@ -70,7 +75,7 @@
class UserSocialAuth(AbstractUserSocialAuth):
"""Social Auth association model"""

class Meta:
class Meta(AbstractUserSocialAuth.Meta):
"""Meta data"""

app_label = "social_django"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_complete(self, mock_request):
def test_disconnect(self, mock_request):
user_model = get_user_model()
user = user_model._default_manager.create_user(username="test", password="pwd")
UserSocialAuth.objects.create(user=user, provider="facebook")
UserSocialAuth.objects.create(user=user, provider="facebook", uid="some-mock-facebook-uid")
self.client.login(username="test", password="pwd")

url = reverse("social:disconnect", kwargs={"backend": "facebook"})
Expand Down
Loading