简体   繁体   中英

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

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

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.

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 deleted 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.

如上面的答案,[null = True] 可用,我建议另一个 [default = None] 也可用

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM