django2.2.7版本對mysql的支持總是報錯 按照下面步驟操作恢復正常使用

# django啓動mysql報錯
## 關於pymysql驅動的錯誤
項目目中的__init__.py,寫入
```
import pymysql
pymysql.install_as_MySQLdb()
```
## django2.2/mysql ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3
- 進入django安裝目錄
/opt/conda373/envs/python37/lib/python3.7/site-packages/django/db/backends/mysql
- 找到base.py文件,註釋掉 base.py 中如下部分(35/36行)
if version < (1, 3, 3):
     raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)
- AttributeError: ‘str’ object has no attribute ‘decode’
找到operations.py文件(46行,版本不同行數不同哈~自個兒find一下),將decode改爲encode
#linux vim 查找快捷鍵:?decode
if query is not None:
    query = query.decode(errors='replace')
return query
#改爲
if query is not None:
    query = query.encode(errors='replace')
return query


## django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint')
```
DATABASES= {    
    'default': {        
        'ENGINE': 'django.db.backends.mysql',        
        'NAME': 'ebusiness',        
        'USER':'root',        
        'PASSWORD':'123456',        
        'HOST':'localhost',        
        'PORT':'3306',        
        'OPTIONS': {            
            "init_command": "SET foreign_key_checks = 0;",        
            }, #加入這個在setting.py文件中數據庫配置信息中        
        #'OPTIONS':{'init_command':'SET engine=InnoDB',}    
        }
        }

```

## django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency account.0001_initial on database 'default'.

把每個app中的migrations文件下全刪除了,重新建庫  
python3 manage.py makemigrations appname  
其中python3 manage.py makemigrations 現在不能一次性建好所有應用了,注意app的先後順序

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章