Make recipe quantity and related key together give me IntegrityError
proofit404 opened this issue · 5 comments
I have this recipes:
from model_mommy.recipe import Recipe, foreign_key
user = Recipe('advance_account.User')
visitor = Recipe('advance_account.Visitor', user=foreign_key(user))
In my tests this code gives me integrity error:
mommy.make_recipe('account.visitor', _quantity=2)
# django.db.utils.IntegrityError: duplicate key value violates unique constraint "advance_account_visitor_user_id_key"
# DETAIL: Key (user_id)=(5) already exists.
But this code works fine:
for i in range(2):
mommy.make_recipe('account.visitor')
Let me know, if I can help somehow.
Thanks @proofit404 and sorry for the delayed reply! I'll try to take a look at it as soon as possible.
@proofit404 I'm closing the issue because I've added a test replicating your scenario and model mommy seems to be working just fine. Take a look at 14de573 commit.
Maybe your model has a more rigorous constraint. Were you using a OneToOneField
instead of a model field?
Hi, thanks!
Will report it more verbosely, if I hit it the second time. I done with those project so do not have access to the code base any more.
class Region(models.Model):
"""
Region
"""
name = models.CharField(verbose_name=_('Region name'), max_length=255, unique=True)
class Model1(models.Model):
region = models.ForeignKey(Region, verbose_name=_('region'), on_delete=models.CASCADE)
class Model2(models.Model):
region = models.ForeignKey(Region, verbose_name=_('region'), on_delete=models.CASCADE)
class Model3(models.Model):
rel_1 = models.ForeignKey('Model1', verbose_name=_('rel1'), on_delete=models.CASCADE)
rel_2 = models.ForeignKey('Model2', verbose_name=_('rel2'), on_delete=models.CASCADE)
recipe_to_model_1 = Recipe(
Model1,
region=foreign_key(recipe_to_region),
)
recipe_to_model_2 = Recipe(
Model2,
region=foreign_key(recipe_to_region),
)
recipe_to_model_3 = Recipe(
Model3,
rel_1=foreign_key(recipe_to_model_1),
rel_2=foreign_key(recipe_to_model_2)
)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "my_app_region_name_key"
DETAIL: Key (name)=(Root region 1) already exists.
In debug:
- Mommy trying to create Model1 instance with recipe. Model1 recipe creates Region
- Mommy trying to create Model2 instance with recipe. Model2 recipe trying to create Region, which already exists.
Please, check this case and reopen this issue if nessesary.
@werevolff, thanks for reporting this. Since is not related to the _quantity and related key together, as described before, would mind opening a new issue? It will help us to keep a better track of the things we need to do. Thank you.