日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

發布時間:2024/4/11 数据库 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 django添加mysql数据库_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

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,允許為空

問題解決

總結

以上是生活随笔為你收集整理的django添加mysql数据库_Django添加mysql数据库关联时出现的错误的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。