I use Iterator to create a strict sequence of values.
class MyModelFactory(DjangoModelFactory):
class Meta:
model = MyModel
django_get_or_create = ("first_field",)
first_field = Iterator(["value_1", "value_2", "value_3", "value_4"])
second_field = Iterator("aaaa", "bbbb", "cccc"])
third_field = Iterator([1, 2, 3, 4])
I expect that result should be next:
[
("value_1", "aaaa", 1),
("value_2", "bbbb", 2),
("value_3", "cccc", 3),
("value_4", "aaaa", 4)
]
But the outcome is unpredictable:
[
("value_1", "aaaa", 4),
("value_2", "bbbb", 3),
("value_3", "cccc", 1),
("value_4", "aaaa", 2)
]
And when I use this MyModelFactory as a RelatedFactory two times in row, my database raise an error duplicate key value violates unique constraint third_field. Yes third_field must be unique. The question is why does this error occur if I use django_get_or_create?
Any ideas?