Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django.db.utils.IntegrityError: NOT NULL constraint failed: app.area_id

Tags:

python

django

When I run ./manage.py migrate,error happens django.db.utils.IntegrityError: NOT NULL constraint failed: app.area_id . models.py is

class Area(models.Model):
    name = models.CharField(max_length=20, verbose_name='area', null=True)

class User(models.Model):
    name = models.CharField(max_length=200,null=True)
    age = models.CharField(max_length=200,null=True)
    area = models.ForeignKey('Area', default="")

class Prefecture(models.Model):
    name = models.CharField(max_length=20, verbose_name='city')
    area = models.ForeignKey('Area')

class City(models.Model):
    name = models.CharField(max_length=20, verbose_name='region')
            prefecture = models.ForeignKey('Prefecture')

class Price(models.Model):
    name = models.CharField(max_length=20, verbose_name='price')
            PRICE_RANGE = (
                ('a', 'under500'),
                ('b', '500-1000'),
                ('c', 'upper1000'),
            )
    price_range = models.CharField(max_length=1, choices=PRICE_RANGE)
    city = models.ForeignKey('City')

When I wrote area = models.ForeignKey('Area'),I got an error You are trying to add a non-nullable field 'area' to transaction without a default; we can't do that (the database needs something to populate existing rows). Please select a fix: 1) Provide a one-off default now (will be set on all existing rows with a null value for this column) 2) Quit, and let me add a default in models.py. What is wrong? How can I fix this?

Now models.py is

class Area(models.Model):
    name = models.CharField(max_length=20, verbose_name='area', null=True)

class User(models.Model):
    name = models.CharField(max_length=200,null=True)
    age = models.CharField(max_length=200,null=True)
    area = models.ForeignKey('Area', default="")

class Prefecture(models.Model):
    name = models.CharField(max_length=20, verbose_name='city')
    area = models.ForeignKey('Area', null=True, blank=True)

class City(models.Model):
    name = models.CharField(max_length=20, verbose_name='region')
            prefecture = models.ForeignKey('Prefecture', null=True, blank=True)

class Price(models.Model):
    name = models.CharField(max_length=20, verbose_name='price')
            PRICE_RANGE = (
                ('a', 'under500'),
                ('b', '500-1000'),
                ('c', 'upper1000'),
            )
    price_range = models.CharField(max_length=1, choices=PRICE_RANGE)
    city = models.ForeignKey('City', null=True, blank=True)

I also got same error.I already tried cache.clear() .

like image 355
user8504021 Avatar asked Oct 17 '22 06:10

user8504021


2 Answers

In the model below you are trying to add area as a ForeignKey. Since this Prefecture table has some data already Django does not know what to add in area field for existing rows.

class Prefecture(models.Model):
    name = models.CharField(max_length=20, verbose_name='city')
    area = models.ForeignKey('Area') # non null-able field Django does
                                     # not know what to add

Simple solution. Provide a default or add null

class Prefecture(models.Model):
        name = models.CharField(max_length=20, verbose_name='city')
        area = models.ForeignKey('Area', null=True)

Note: for all existing rows in Prefecture now area field will be null. You can add this field now for new rows.

like image 185
Arpit Solanki Avatar answered Oct 21 '22 00:10

Arpit Solanki


You'll first need to delete the previously generated migration file.

Otherwise, even if you fix your models.py, you will keep getting the same error because ./manage.py migrate will keep trying to apply the previous migration.

After you delete the previously generated migration file, use the following:

area = models.ForeignKey('Area', blank=True, null=True)

This will make the area attribute optional, and you will be able to generate a new migration file and proceed with ./manage.py migrate.

After you've done that, you can optionally write a data migration script to fill in the missing area attributes. You can read more about in in the documentation.

You can also fill-in this attribute manually for your existing data, instead of writing a migration script (for instance, buy directly updating your database or by using django-admin).

Once you make sure that all your data has the area attribute filled-in, you can make your final change:

area = models.ForeignKey('Area')

When you apply this migration, you will simply add a NOT NULL constraint to this field in your database, and it should be OK if you filled-in the data in the previous step.

like image 42
alexpirine Avatar answered Oct 21 '22 00:10

alexpirine