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

在 Django 2.0 版本以后,为外键和一对一关系添加 `on_delete` 参数是必需的,以避免数据不一致问题。错误 `TypeError: __init__() missing 1 required positional argument: 'on_delete'` 提示需要提供 `on_delete=models.CASCADE`。此外,尝试迁移时若未设置默认值或允许为空,会出现数据库需要填充现有行的问题。解决方案是为字段如 `author` 设置 `null=True` 和 `on_delete=models.CASCADE`。通常情况下,`CASCADE` 是合适的选择。
摘要由CSDN通过智能技术生成

添加关联时出现的错误1

PS C:\Users\Administrator\Desktop\04作业\day4> python manage.py makemigrations

Traceback (most recent call last):

File "manage.py", line 15, in

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 "", line 994, in _gcd_import

File "", line 971, in _find_and_load

File "", line 955, in _find_and_load_unlocked

File "", line 665, in _load_unlocked

File "", line 678, in exec_module

File "", line 219, in _call_with_frames_removed

File "C:\Users\Administrator\Desktop\04作业\day4\index\models.py", line 53, in

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,允许为空

问题解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值