[Django]-Django: AttributeError: 'UniqueConstraint' object has no attribute 'fields_orders'

4👍

You defined this in the indexes parameter, but this should be in the constraints [Django-doc] attribute. indexes contains, as the name suggests, the list of indexes defined on the table. constraints on the other hand contains the constraints that should be enforced.

class Employment(models.Model):
    Employment_ID = models.AutoField(primary_key=True)
    user = models.ForeignKey(User, db_column='User_ID', on_delete=models.CASCADE)
    company = models.ForeignKey(Company, db_column='Company_ID', on_delete=models.CASCADE)

    class Meta:
        db_table = "Employments"
        constraints = [
            models.UniqueConstraint(
                fields=['User_ID', 'Company_ID'],
                name='Employment_User_Company_UNIQUE'
            )
        ]

Leave a comment