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

Merged
merged 1 commit into from
May 23, 2025
Merged
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(
condition=models.Q(("uid", ""), _negated=True),
Copy link
Member

Choose a reason for hiding this comment

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

Fails with TypeError: __init__() got an unexpected keyword argument 'condition'.

Perhaps need a more recent Django version and the dependencies need to be adjusted?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, i'll need to use check to support 3.2 i see.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hm, i have check in the models.py but django generated condition because i used a newer django generating the migration. I'll modify it by hand to use check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

name="user_social_auth_uid_required",
),
),
]
3 changes: 2 additions & 1 deletion social_django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __str__(self):
return str(self.user)

class Meta:
constraints = [models.CheckConstraint(condition=~models.Q(uid=""), name="user_social_auth_uid_required")]
abstract = True

@classmethod
Expand Down Expand Up @@ -67,7 +68,7 @@ def user_model(cls):
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