berinhard/model_mommy

Error when upgrading to Django 1.11

Closed this issue · 7 comments

After upgrading to django 1.11 I am getting the following error:

Traceback (most recent call last):
  File "/Users/max/projects/project/assets/tests/test_questions.py", line 61, in setUp
    self.question = mommy.make(QuestionGroup, asset=asset)
  File "/Users/max/projects/env/lib/python3.5/site-packages/model_mommy/mommy.py", line 54, in make
    return mommy.make(_save_kwargs=_save_kwargs, **attrs)
  File "/Users/max/projects/env/lib/python3.5/site-packages/model_mommy/mommy.py", line 227, in make
    return self._make(commit=True, commit_related=True, _save_kwargs=_save_kwargs, **attrs)
  File "/Users/max/projects/env/lib/python3.5/site-packages/model_mommy/mommy.py", line 263, in _make
    instance = self.instance(self.model_attrs, _commit=commit, _save_kwargs=_save_kwargs)
  File "/Users/max/projects/env/lib/python3.5/site-packages/model_mommy/mommy.py", line 287, in instance
    instance.save(**_save_kwargs)
  File "/Users/max/projects/env/lib/python3.5/site-packages/django/db/models/base.py", line 812, in save
    force_update=force_update, update_fields=update_fields)
  File "/Users/max/projects/env/lib/python3.5/site-packages/django/db/models/base.py", line 842, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/Users/max/projects/env/lib/python3.5/site-packages/django/db/models/base.py", line 918, in _save_table
    raise DatabaseError("Save with update_fields did not affect any rows.")
django.db.utils.DatabaseError: Save with update_fields did not affect any rows.

The offending code looks like this:

    asset = mommy.make(Asset)
    question = mommy.make(QuestionGroup, asset=asset)    # this is the line that fails

Interestingly if I update the second line to the following the error is no longer triggered.

    mommy.make(QuestionGroup, asset=asset, _save_kwargs={'force_insert': True})

Hello @maximilianhurl

Can you specify the database and model_mommy version being used as well?

Thanks

  • postgres (PostgreSQL) 9.6.2
  • psycopg2 2.7.1
  • model_mommy 1.3.2 (But I also tried the previous version and master)

@marfire try development branch as well, please

Same error when using the latest commit on the development branch (git+https://github.com/vandersonmota/model_mommy@eea72b6683b040764806c287aaeee3eba1bc968a).

can you provide more details about the models schema?

Here's an example of the two models mentioned:


class QuestionGroup(models.Model):   # this is the one that fails to be created
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    created = models.DateTimeField(auto_now_add=True, db_index=True)
    modified = models.DateTimeField(auto_now=True)
    asset = models.ForeignKey(Asset, related_name='question_groups')
    text = models.CharField(max_length=255)
    position = PositionField(collection='asset', default=0)


class Asset(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    created = models.DateTimeField(auto_now_add=True, db_index=True)
    modified = models.DateTimeField(auto_now=True)
    name = models.CharField(max_length=255)
    # ... lots more similar fields

Turns out the issue was related to a problem in https://github.com/jpwatts/django-positions which has now been fixed.