Reputation: 1962
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????
Upvotes: 28
Views: 47456
Reputation: 75
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.
Upvotes: 0
Reputation: 610
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=''
Upvotes: 0
Reputation: 592
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
general_app/migrations/*.py
, except __init__.py
, be careful.You could check if the problem was fixed, if not:
mysql> DROP DATABASE <name_db>
)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
Upvotes: 0
Reputation: 549
Here is what I do:
requirement = models.ForeignKey(Requirement)
to requirement = models.ForeignKey(Requirement, null=True)
requirement = models.ForeignKey(Requirement, null=True)
to requirement = models.ForeignKey(Requirement)
I guess the rule of django-orm about foreignKey is:
Upvotes: 1
Reputation: 1553
Here is what I did to fix the same issue
Hope this helps. This solution will work everytime you face these kinds of error.
Upvotes: 2
Reputation: 572
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!
Upvotes: 13
Reputation: 11879
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>)
Upvotes: 43