The Integrity error means that there is a row in the database with role = 1
So another row with role=1 would break the unique constraint and is not
allowed.

So either remove the unique constraint or make sure it will be unique.

Den tis 9 apr. 2019 13:29Ryan Jarvis <cabal...@gmail.com> skrev:

> Hey there,
>
> I'm trying out the new UniqueConstraint functionality in Django 2.2 and
> seem to be misunderstanding something.  When adding a Constraint to the
> model I am getting an uncaught IntegrityError in the admin.
> I've got the following sample code:
>
> *models.py*
>
> class Seminar(models.Model):
>     seminar_id = models.CharField(max_length=255, unique=True)
>     members = models.ManyToManyField(User, through='SeminarRole',
> related_name="studies")
>
> class SeminarRole(models.Model):
>     LEAD = 1  # Only 1 Lead permitted per seminar
>     SUPPORT = 2
>     ROLE_CHOICES = (
>         (LEAD, 'Lead'),
>         (SUPPORT, 'Support'),
>     )
>
>     user = models.ForeignKey(User, related_name='seminar_roles',
> on_delete=models.CASCADE)
>     seminar = models.ForeignKey('seminar', related_name='roles',
> on_delete=models.CASCADE)
>
>     role = models.IntegerField(choices=ROLE_CHOICES)
>
>     class Meta:
>         constraints = [
>             models.UniqueConstraint(fields=['seminar'],
> condition=Q(role=1), name="only_one_lead"),
>         ]
>
>
>
> For the code above in the Django Admin I can successfully add a
> SeminarRole with User1 as the Lead for SeminarA, User2 as the Lead for
> SeminarB, and User1 as Support for SeminarB but if I try and add User2 as
> another Lead to SeminarA it gives me an Exception.  Should I be seeing the
> Django Admin catch this before hand?
>
> IntegrityError at /admin/study_management/seminarrole/add/
> duplicate key value violates unique constraint "only_one_lead"
> DETAIL:  Key (seminar_id)=(1) already exists.
>
>
> I'm on Django 2.2, Python 3.7 and Postgres 11.2
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/fb2ab520-178d-4a0c-acf7-8cb0498936e4%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/fb2ab520-178d-4a0c-acf7-8cb0498936e4%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAPLZMbPxNN%3DCa6GqjcR_kXDO_a2VyzKxgVNfTgzAdtnfhhYqgA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to