ManyToMany relationships referencing 'self' choke when the model has a non null foreign key

Bug #915264 reported by Nicola Heald
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Django Factory
Fix Released
High
James Westby

Bug Description

models:

  class Group(models.Model):
      name = models.CharField(max_length=64)

  class Member(models.Model):
      group = models.ForeignKey(Group)
      name = models.CharField(max_length=64)
      friends = models.ManyToManyField('self', blank=True, symmetrical=False, related_name='buddies')

>>> from django_factory import Factory
>>> f = Factory()
>>> from testapp.models import Member
>>> m = f.make_one(Member)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/vagrant/Work/django_factory_test/apps/django_factory/factory.py", line 493, in make_one
    return self._create_one(model, True, [], attrs)
  File "/vagrant/Work/django_factory_test/apps/django_factory/factory.py", line 413, in _create_one
    instance, m2m_dict, fk_dict = self._generate_instance(model, save, attrs, not_including)
  File "/vagrant/Work/django_factory_test/apps/django_factory/factory.py", line 283, in _generate_instance
    model, field, not_including, attrs)
  File "/vagrant/Work/django_factory_test/apps/django_factory/factory.py", line 199, in _generate_many_to_many_value
    not_including + [model], {}))
  File "/vagrant/Work/django_factory_test/apps/django_factory/factory.py", line 419, in _create_one
    instance.save()
  File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line 410, in save
    self.save_base(force_insert=force_insert, force_update=force_update)
  File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line 495, in save_base
    result = manager._insert(values, return_id=update_pk)
  File "/usr/lib/pymodules/python2.6/django/db/models/manager.py", line 177, in _insert
    return insert_query(self.model, values, **kwargs)
  File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 1087, in insert_query
    return query.execute_sql(return_id)
  File "/usr/lib/pymodules/python2.6/django/db/models/sql/subqueries.py", line 320, in execute_sql
    cursor = super(InsertQuery, self).execute_sql(None)
  File "/usr/lib/pymodules/python2.6/django/db/models/sql/query.py", line 2369, in execute_sql
    cursor.execute(sql, params)
  File "/usr/lib/pymodules/python2.6/django/db/backends/util.py", line 19, in execute
    return self.cursor.execute(sql, params)
IntegrityError: null value in column "group_id" violates not-null constraint

What happens is that the member is created, a group is created, and that's fine. Then a member is created to get test data in to the 'friends' field, but none of the foreignkeys are used when generating the member for 'friends'.

Revision history for this message
James Westby (james-w) wrote :

Hi, nice bug. The loop detection was broken (over eager) and so it was skipping
the ForeignKey as you identified.

I've fixed the loop detection so it only skips the ForeignKey if it is really part
of a loop.

Thanks,

James

Changed in django-factory:
status: New → Fix Committed
importance: Undecided → High
assignee: nobody → James Westby (james-w)
James Westby (james-w)
Changed in django-factory:
milestone: none → 0.7
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.