27

I defined below model and getting

error : You are trying to add a non-nullable field 'user' to videodata without a default; we can't do that

models.py

class User(Model):
    userID = models.IntegerField()
    userName = models.CharField(max_length=40)
    email = models.EmailField()
    class Meta:
        ordering = ['userName']
        verbose_name = 'User MetaData'
        verbose_name_plural = 'Users MetaData'
    def __unicode__(self):
        return str(self.userName)

class VideoData(Model):
    video = models.CharField(max_length=40)
    time  = models.IntegerField()
    user = models.ForeignKey(User, related_name='User')
    class Meta:
        verbose_name = 'User_Video MetaData'

Where i am doing wrong????

1

7 Answers 7

42

As the error says, your user field on VideoData is not allowing nulls, so you either need to give it a default user or allow nulls. Easiest way is to allow nulls.

user = models.ForeignKey(User, related_name='User', null=True)

or have a default user

user = models.ForeignKey(User, related_name='User', default=<have your default user id here>)
6
  • 6
    On a database level, a foreign-keyed column requiring a default value is completely flawed and wrong. Django forces you to make your data architecture wrong. Also no support for composite foreign-keys. Such a damn shame. Apr 21, 2019 at 12:58
  • 2
    I gave two options on how to solve the problem. Django isn't forcing a required default. I can think of plenty of situations where it would be acceptable to add a default foreign key. Apr 22, 2019 at 14:04
  • 1
    Sad that this is the only viable solution. May 22, 2020 at 0:08
  • @FeralessFuture I am curious about the sort of solution that you would think of as not "sad". May 22, 2020 at 7:19
  • 1
    Noobie here--Why would this be a problem if you're just defining the model? If there is no pre-existing data, what does a FK default even mean?
    – xtian
    Jan 30, 2021 at 19:53
13

I have run into the same problem with my OneToOneField. And, what I did was to delete all the migration files (which are under the directory of migrations under your app), and ran:

python manage.py makemigrations

and

python manage.py migrate

I don't know why, but it worked in my case. It won't hurt you to try what I wrote above.

Good luck!

4
  • 5
    This works well if it is for your local enviroment with meaningless data, but if it was for production you'd have to apply a default wanting it or not..
    – R.R.C.
    May 10, 2018 at 7:17
  • 1
    We can delete migrations for learning projects. In Live projects it would not be advisable to delete migration files. Should be some other solution as well. Jul 31, 2019 at 9:04
  • 7
    -1. This is dangerous advice that will break your migrations and DB state. Resetting the migrations will accomplish the purpose, yes (because you are forcing django to think that the DB is empty) but it will lose the synchronization between the migration files and the DB migration state. If you changed your models without migrating before doing this, you will also find that django doesn't detect the changes anymore and now you're left with a broken model and consistency errors ready to appear at the next migration attempt. If you really want to do this, you have to nuke the DB and recreate it. Feb 7, 2020 at 16:25
  • 1
    -1 As @soxwithMonica pointed out, deleting migration files should never be suggested as a solution. It is nothing but deleting your existing DB and recreating it - blank!.
    – Rk..
    Mar 21, 2022 at 10:44
2

Here is what I did to fix the same issue

  1. Comment the foreign key reference in videodata and run the makemigrations and migrate
  2. Add the model in admins.py and make a new entry in table
  3. Now uncomment the foreign key reference and provide a default=1 .Run make migrations and migrate
  4. Remove the default=1 in foreign key field.

Hope this helps. This solution will work everytime you face these kinds of error.

0

Here is what I do:

  1. change requirement = models.ForeignKey(Requirement) to requirement = models.ForeignKey(Requirement, null=True)
  2. run makemigrations and migrate
  3. change back requirement = models.ForeignKey(Requirement, null=True) to requirement = models.ForeignKey(Requirement)
  4. run makemigrations and migrate again.

I guess the rule of django-orm about foreignKey is:

  1. allow foreignKey to be null when first create table
  2. reject to add a foreignKey which is null, because the foreignKey of the data insert into table before will be null, which against programmer's will
  3. allow to change foreignKey from not null to null.
0

The problem sometimes happens when you make a lot of changes in your models.py file (in my case It was related to an ImageField non-nullable field). Rather than field problems indeed. One solution in django 2.0.2 , python 3.6.4 , mysql 5.7.21

  • Delete all files inside migrations folder general_app/migrations/*.py, except __init__.py, be careful.

You could check if the problem was fixed, if not:

  • Repeat the above step, and
  • Delete your database (e.g. in mysql mysql> DROP DATABASE <name_db>)
  • Now create a new one with the same name mysql> CREATE DATABASE <name_db>.

These steps fixed the issue in my case. Now you could run without mistakes:

$ python manage.py makemigrations
$ python manage.py migrate
1
  • If you want to do this you need to recreate the DB (backup with dumpdata), otherwise you will end with broken migrations. Note that recreating the DB may not be an option in production. Feb 7, 2020 at 16:26
0

I have faced the same issue: ...non-nullable field 'user' to videodata...

model: Videodata

field: user

just add one more attr in the field user of the model Videodata: default=''

0

The problem happens when I use TextField(). I found my previous migration file somehow overwrites new fields. Cleaning previous migration files before re-migrating happens resolve the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.