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>)
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!
- [Django]-Scoped_session(sessionmaker()) or plain sessionmaker() in sqlalchemy?
- [Django]-Django: Adding CSS classes when rendering form fields in a template
- [Django]-Django: Using an F expression for a text field in an update call
2👍
Here is what I did to fix the same issue
- Comment the foreign key reference in videodata and run the makemigrations and migrate
- Add the model in admins.py and make a new entry in table
- Now uncomment the foreign key reference and provide a default=1 .Run make migrations and migrate
- Remove the default=1 in foreign key field.
Hope this helps.
This solution will work everytime you face these kinds of error.
- [Django]-How to use custom AdminSite class?
- [Django]-DateTimeField doesn't show in admin system
- [Django]-Cannot access django app through ip address while accessing it through localhost
0👍
Here is what I do:
- change
requirement = models.ForeignKey(Requirement)
torequirement = models.ForeignKey(Requirement, null=True)
- run makemigrations and migrate
- change back
requirement = models.ForeignKey(Requirement, null=True)
torequirement = models.ForeignKey(Requirement)
- run makemigrations and migrate again.
I guess the rule of django-orm about foreignKey is:
- allow foreignKey to be null when first create table
- 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
- allow to change foreignKey from not null to null.
- [Django]-Django migration error :you cannot alter to or from M2M fields, or add or remove through= on M2M fields
- [Django]-Web application monitoring best practices
- [Django]-Using a Django custom model method property in order_by()
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
- [Django]-How to I show a list of ForeignKey reverse lookups in the DJango admin interface?
- [Django]-Bulk update for many to many fields
- [Django]-Cleaning up a database in django before every test method
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=”
- [Django]-How to get primary keys of objects created using django bulk_create
- [Django]-Django admin – make all fields readonly
- [Django]-How to add the current query string to an URL in a Django template?
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.
- [Django]-Gunicorn + nginx: Server via socket or proxy?
- [Django]-How to disable admin-style browsable interface of django-rest-framework?
- [Django]-Naming Python loggers