1

I have using django-autofixture to generate some random data for my database instance. My model is as follows:

class DummyModel(models.Model):
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=150, blank=True)
    time_points = models.PositiveIntegerField()
    more_text = models.CharField(max_length=100, blank=True)
    image_type = models.ForeignKey(ImageTypeModel, null=False, blank=False,
                                   default='')

    class Meta:
        db_table = "dummy"

So, I have a foreign key which should always be present and it cannot be NULL. I also have created instances for this ImageTypeModel using the fixtures functionality in Django. I deleted everything and migrated fresh and verified that the ImageTypeModel database table exists and is populated. The ImageTypeModel is defined as:

class ImageTypeModel(models.Model):
    name = models.CharField(max_length=100)
    dims = models.IntegerField()

    class Meta:
        db_table = "imagetypes"

    def __str__(self):
        return self.name

    def __unicode__(self):
        return self.name

Now I attempt to create some random data using the command:

python manage.py loadtestdata myapp.DummyModel:30

However, this returns me this error:

django.db.utils.IntegrityError: NOT NULL constraint failed: dummy.image_type_id

I am at a loss as to why it should be and have tried deleting/replicating the project to a fresh location but no go.

Luca
  • 10,458
  • 24
  • 107
  • 234
  • Check [this question](https://stackoverflow.com/questions/937954/how-do-you-specify-a-default-for-a-django-foreignkey-model-or-adminmodel-field). It might answer your question. – Elmer Esque Jun 30 '17 at 15:31

0 Answers0