Django: one-to-one field with factory_boy: UNIQUE constraint failed
<p>I'm trying to make factories for these models with factory_boy:</p>

<pre><code>class Course(models.Model):
    group = models.OneToOneField(Group)
    title = models.CharField(max_length=1024)


class CourseInfo(models.Model):
    course = models.OneToOneField(Course, related_name='info')
    starttime = models.TimeField(null=True, blank=True)
    endtime = models.TimeField(null=True, blank=True)
</code></pre>

<p>Here are my factories:</p>

<pre><code>class CourseInfoFactory(factory.DjangoModelFactory):
    class Meta:
        model = CourseInfo

    course = factory.SubFactory('courseaffils.tests.factories.CourseFactory')
    starttime = time(hour=4)
    endtime = time(hour=6)


class CourseFactory(factory.DjangoModelFactory):
    class Meta:
        model = Course

    title = factory.Sequence(lambda n: 'Example Course %d' % n)
    group = factory.SubFactory(GroupFactory)
    info = factory.RelatedFactory(CourseInfoFactory, 'course')
</code></pre>

<p>Now, when I call <code>CourseFactory()</code> or <code>CourseInfoFactory()</code> I get an IntegrityError:</p>

<blockquote>
  <p>IntegrityError: UNIQUE constraint failed: courseaffils_courseinfo.course_id</p>
</blockquote>

<p>What's wrong with my factories? How can I fix this problem?</p>

<p>If I instead do <code>course = factory.RelatedFactory('courseaffils.tests.factories.CourseFactory')</code> on the CourseInfoFactory, I get this error, as expected:</p>

<blockquote>
  <p>IntegrityError: NOT NULL constraint failed: courseaffils_courseinfo.course_id</p>
</blockquote>

<p>If you need more details, here's the actual code I'm working with:</p>

<p><a href="https://github.com/ccnmtl/django_courseaffils/blob/master/courseaffils/models.py" rel="noreferrer">https://github.com/ccnmtl/django_courseaffils/blob/master/courseaffils/models.py</a></p>