Django添加mysql数据库关联时出现的错误

添加关联时出现的错误1
PS C:\Users\Administrator\Desktop\04作业\day4> python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "D:\Program Files\Python36\lib\site-packages\django-2.0.6-py3.6.egg\django\core\management\__init__.py", line 371, in ex
ecute_from_command_line
utility.execute()
File "D:\Program Files\Python36\lib\site-packages\django-2.0.6-py3.6.egg\django\core\management\__init__.py", line 347, in ex
ecute
django.setup()
File "D:\Program Files\Python36\lib\site-packages\django-2.0.6-py3.6.egg\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "D:\Program Files\Python36\lib\site-packages\django-2.0.6-py3.6.egg\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "D:\Program Files\Python36\lib\site-packages\django-2.0.6-py3.6.egg\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "D:\Program Files\Python36\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Administrator\Desktop\04作业\day4\index\models.py", line 53, in <module>
class Wife(models.Model):
File "C:\Users\Administrator\Desktop\04作业\day4\index\models.py", line 57, in Wife
author = models.OneToOneField(Author,null=True)
TypeError: __init__() missing 1 required positional argument: 'on_delete'
添加关联时出现的错误2
PS C:\Users\Administrator\Desktop\04作业\day4> python manage.py makemigrations
You are trying to change the nullable field 'author' on wife to non-nullable 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) Ignore for now, and let me handle existing rows with NULL myself (e.g. because you added a RunPython or RunSQL operation to
handle NULL values in a previous data migration)
3) Quit, and let me add a default in models.py
Select an option:

解决方法:
author = models.OneToOneField(Author,null=True,on_delete=models.CASCADE)

错误1原因:
在django2.0后,定义外键和一对一关系的时候需要加on_delete选项,此参数为了避免两个表里的数据不一致问题,不然会报错:
TypeError: __init__() missing 1 required positional argument: 'on_delete'
举例说明:
user=models.OneToOneField(User)
owner=models.ForeignKey(UserProfile)
需要改成:
user=models.OneToOneField(User,on_delete=models.CASCADE) --在老版本这个参数(models.CASCADE)是默认值
owner=models.ForeignKey(UserProfile,on_delete=models.CASCADE) --在老版本这个参数(models.CASCADE)是默认值
参数说明:
on_delete有CASCADE、PROTECT、SET_NULL、SET_DEFAULT、SET()五个可选择的值
CASCADE:此值设置,是级联删除。
PROTECT:此值设置,是会报完整性错误。
SET_NULL:此值设置,会把外键设置为null,前提是允许为null。
SET_DEFAULT:此值设置,会把设置为外键的默认值。
SET():此值设置,会调用外面的值,可以是一个函数。
一般情况下使用CASCADE就可以了。

错误2原因
添加属性的时候没有为属性设置默认值,也没有设置允许为空,这里将属性值内容设置null=True,允许为空
问题解决

猜你喜欢

转载自www.cnblogs.com/mzy1111/p/9262785.html