rahnama7m
rahnama7m

Reputation: 977

TypeError: 'UniqueConstraint' object is not iterable in Django

When I set multiple unique fields with UniqueConstraint:

class Meta:
    constraints = (models.UniqueConstraint(fields=['student', 'classroom', 'code'], name='student_classroom_code'))

and run python manage.py makemigrations Raise this error:

TypeError: 'UniqueConstraint' object is not iterable

What is wrong with this?

Upvotes: 3

Views: 1319

Answers (3)

Jorge Luis
Jorge Luis

Reputation: 957

Python tuples need at least one , before closing. If you dont specify this ,, the interpreter take it for a group and the type will be the var inside the parenthesis

Example:

t = ('i am a tuple',)         # this is good
t = ('i am not a tuple')      # this is bad

Upvotes: 0

Martin Sedlář
Martin Sedlář

Reputation: 91

You need to assign iterable to the constraints. You are missing , in (models.UniqueConstraint(...),), which means you are assigning models.UniqueConstraint instance instead of tuple.

class Meta:
    constraints = (models.UniqueConstraint(fields=['student', 'classroom', 'code'], name='student_classroom_code'),)

Upvotes: 6

AzyCrw4282
AzyCrw4282

Reputation: 7744

The error simply means that it is not iterable. Try to define it like this

For example

class Meta:
    constraints = [models.UniqueConstraint(fields['app_uuid', 'version_code'], name='unique appversion')]

Upvotes: 2

Related Questions