0

My table is defined as below.

class Role(models.Model):

    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        to_field="email",
        db_column="user",
        on_delete=models.CASCADE
    )

    vertical = models.ForeignKey(
        Verticals,
        to_field="name",
        db_column="vertical",
        on_delete=models.CASCADE
    )

    product_domain = models.ForeignKey(
        ProductDomains,
        to_field="name",
        db_column="product_domain",
        null=True,
        on_delete=models.CASCADE
    )

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=[
                'user',
                'vertical',
                'product_domain'
            ],
                name='unique-permissions-per-user')
        ]

Here are the migrations that are generated

migrations.CreateModel(
            name='Role',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('product_domain', models.ForeignKey(db_column='product_domain', null=True, on_delete=django.db.models.deletion.CASCADE, to='verticals.ProductDomains', to_field='name')),
                ('user', models.ForeignKey(db_column='user', on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, to_field='email')),
                ('vertical', models.ForeignKey(db_column='vertical', on_delete=django.db.models.deletion.CASCADE, to='verticals.Verticals', to_field='name')),
            ],
        ),
        migrations.AddConstraint(
            model_name='role',
            constraint=models.UniqueConstraint(fields=('user', 'vertical', 'product_domain'), name='unique-permissions-per-user'),
        ),

Serializers for the Role model is

class RoleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Role
        fields = '__all__'

Here is the interactive console for the same

Link to console image (Unable to add due to less reputation )

Here the UniqueConstraint is not working, why? I use models.UniqueConstraint in the same project many times but it doesn't work in this case.

My configuration is Django - 3.0.4 Django Rest Framework - 3.11.0 Database - MySql

Please help and ask if any information is missing out.

adshin21
  • 178
  • 2
  • 9
  • 1
    Please add much more information on what's not working. Are you seeing an error? Or can you show a shell session where you insert two colliding records? Did migrations work fine? – Adam Johnson Apr 14 '20 at 08:13
  • Hello @AdamJohnson, I updated the question with more info kindly check it and ask if anything missing, as for that I'm not getting any error, the same record is being inserted into DB and yes migrations work fine. – adshin21 Apr 14 '20 at 11:04

1 Answers1

2

Your logs don't show any attempt to insert the same record into the DB twice, just validation.

It appears Django Rest Framework does not import constraints from UniqueConstraint, and only has code to validate with the old Meta.unique_together constraint definition.

There is a tracking issue, ModelSerializer not generating validators for constraints, on the DRF GitHub where you can follow the latest updates on adding support for UniqueConstraint validation.

kcontr
  • 343
  • 2
  • 12
Adam Johnson
  • 471
  • 1
  • 5
  • 11