原创

【Django】配置MySQL时报错

 2020年1月9日 16:20
【Django】配置MySQL时报错

官方文档片段:https://docs.djangoproject.com/en/2.0/topics/install/#get-your-database-running

运行环境

  • Python3.7
  • Django2.2.6
  • Mysql5.7
  1. 下载mysqlclient

    pip install mysqlclient
    
  2. 修改setting.py的配置

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'demo04',
            'USER': 'root',
            'PASSWORD': 'root',
            'HOST': 'localhost',
            'PORT': 3306,
        }
    }
    
  3. 执行迁移命令吧

    python manage.py makemigrations
    python manage.py migrate
    

django.dgrammingError: (1146, u"Table'' doesn't exist")

参考文章:https://stackoverflow.com/questions/27583744/django-table-doesnt-exist

python manage.py makemigrations
python manage.py migrate --fake

注释掉模型

python manage.py makemigrations
python manage.py migrate

Django | 模型类变更后生成迁移文件,报错:You are trying to add a non-nullable field 'BookName' to BookInfo without a default....

报错:

You are trying to add a non-nullable field 'BookName' to BookInfo 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)
  2. Quit, and let me add a default in models.py

原因:

在模型类中添加了一个非空字段,而该模型类对应的表中,已经存在数据,而这些现存的数据并没有新增字段的值

you are trying to add a non-nulllable field xxx to xxx without a default

新增字段时报错,具体的意思,就是说该字段为非空字段,在字段出添加null=True

django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syn tax to use near 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED' at line 1")

在生成迁移文件时python manage.py makemigrations,报错:django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syn tax to use near 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED' at line 1")

运行环境

  • CentOS:6.5
  • MySQL:5.1
  • Django:2.1.1
  • Python:3.7
DATABASES = {
    'default':
        {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'autotest',
            'USER': 'root',
            'PASSWORD': '123456',
            'HOST': '127.0.0.1',
            'PORT': '3306',
            'OPTIONS': {'isolation_level': None} # 加这句
        }
}
  • 如果修改完,不起作用,那就是mysql版本太低了,不支持mysql5.5版本