5

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() .

user8504021
  • 303
  • 5
  • 23

4 Answers4

2

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.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Arpit Solanki
  • 9,567
  • 3
  • 41
  • 57
  • thx,ur comments.I tried ur codes,but I got same error.I updated my question,if u know something please help me. – user8504021 Sep 07 '17 at 05:51
  • in Prefecture model you still have `area = models.ForeignKey('Area')` you have to change it to this `area = models.ForeignKey('Area', null=True, blank=True)` @user8504021 – Arpit Solanki Sep 07 '17 at 05:55
  • I tried ur codes,but i got same error.I updated myquestion code,if u find still some error ,please tell me. – user8504021 Sep 07 '17 at 06:09
  • I can think of only one error now In `User` model change this `area = models.ForeignKey('Area', default="")` to `area = models.ForeignKey('Area', null=True, blank=True)` If it still does not work try a fresh migrate by deleting the db – Arpit Solanki Sep 07 '17 at 06:12
2

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.

alexpirine
  • 3,023
  • 1
  • 26
  • 41
  • 1
    Deleting the previous ("un-commited") migration files was the key! – Jet Blue Feb 03 '21 at 02:42
  • You can specify `default=someUserId` instead of `null=True` if you want to assign a default value to items already in the database that were created before this field was added to the model. – Jet Blue Feb 03 '21 at 02:49
0

as the above answer, [null = True] is available, i recommend another [default = None] is also available

turbo73
  • 1
  • 1
0

It may be possible you have not executed makemigration and migrate.

I faced below error:

django.db.utils.IntegrityError: NOT NULL constraint failed: adminapp_responses.created_by_id

But after makemigration and migrate, it is gone.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68