Python 错误:“您正在尝试添加不可为空的字段”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31357346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error : "You are trying to add a non-nullable field"
提问by Naresh
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
错误 : You are trying to add a non-nullable field 'user' to videodata without a default; we can't do that
models.py
模型.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????
我哪里做错了????
采纳答案by wobbily_col
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.
正如错误所说,您在 VideoData 上的用户字段不允许空值,因此您需要为其提供默认用户或允许空值。最简单的方法是允许空值。
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>)
回答by btaek
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:
我的 OneToOneField 遇到了同样的问题。而且,我所做的是删除所有迁移文件(migrations
在您的应用程序下的目录下),然后运行:
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!
祝你好运!
回答by allsyed
Here is what I did to fix the same issue
这是我为解决相同问题所做的工作
- Comment the foreign key reference in videodataand run the makemigrations and migrate
- Add the model in admins.pyand 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=1in foreign key field.
- 注释videodata 中的外键引用并运行 makemigrations 和 migrate
- 在admins.py 中添加模型并在表中创建一个新条目
- 现在取消注释外键引用并提供default=1.Run make migrations and migrate
- 删除外键字段中的default=1。
Hope this helps. This solution will work everytime you face these kinds of error.
希望这可以帮助。每当您遇到此类错误时,此解决方案都会起作用。
回答by Guihai
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.
- 更改
requirement = models.ForeignKey(Requirement)
为requirement = models.ForeignKey(Requirement, null=True)
- 运行 makemigrations 并迁移
- 变回
requirement = models.ForeignKey(Requirement, null=True)
至requirement = models.ForeignKey(Requirement)
- 运行 makemigrations 并再次迁移。
I guess the rule of django-orm about foreignKey is:
我猜 django-orm 关于 foreignKey 的规则是:
- 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.
- 首次创建表时允许外键为空
- 拒绝添加一个为空的外键,因为之前插入表的数据的外键将为空,这违背了程序员的意愿
- 允许将外键从非空更改为空。
回答by Kurian Benoy
Most of these kind of situtions are not a problem of not adding files to model (using makemigrations and migrate)
大多数此类情况都不是不向模型添加文件的问题(使用 makemigrations 和 migrate)
But due to improper errors in type of Fields models and forms(HERE it was due to inproper syntax error of Foreign Key)
但是由于 Fields 模型和表单的类型错误(这里是由于外键的语法错误不正确)
So first refer:https://www.djangoproject.com/
所以先参考:https: //www.djangoproject.com/
There can be other reasons as Well :
可能还有其他原因:
Sometimes database get populated and it will only accept Non Nullable Value (This error is quite frequent in new versions of django
有时数据库被填充,它只会接受非空值(这个错误在新版本的 django 中很常见
SO also set condition (null==True) in Your model
所以还在你的模型中设置条件 (null==True)
回答by christianbueno.1
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
当您对 models.py 文件进行大量更改时,有时会出现问题(在我的情况下,它与 ImageField 不可为空字段有关)。而不是领域问题。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.
- 删除 migrations 文件夹中的所有文件
general_app/migrations/*.py
,除了__init__.py
,要小心。
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>
.
- 重复以上步骤,然后
- 删除您的数据库(例如在 mysql 中
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
回答by joe-khoa
I have faced the same issue: ...non-nullable field 'user' to videodata...
我遇到了同样的问题:...非空字段“用户”到视频数据...
model: Videodata
型号:Videodata
field: user
领域:用户
just add one more attr in the field user of the model Videodata: default=''
只需在模型 Videodata 的字段 user 中再添加一个 attr:default=''