I have a model:
class NotificationSettings(models.Model): android_device = models.ForeignKey( 'users.AndroidDevice', default=None, null=True, blank=True, on_delete=models.SET_NULL ) user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True) ... class Meta: unique_together = ("user", "android_device")
My problem is that I have unique_together on fields that are nullable. I learned that in PostgreSQL (and generally in the SQL standard) NULL != NULL, so I can end up with, for example, two NotificationSettings objects that have the same device_id and user is NULL in both cases.
I thought that using
NotificationSettings.objects.get_or_create() everywhere I create these objects would suffice but I guess there is a race condition when two request are hitting the endpoint in almost the same time, and I end up with duplicates anyway.
This is why I wanted to make this constraint on the PostgreSQL level and was thinking about changing the user field to not being nullable and having default user instead.
But I feel like creating default user might have some kind of security consequences.
So my question is: Is this a good practice (or practice at all) to create such a sentinel/default user object? Are there any caveats/security risks?</div