错误信息
File "D:\pythonProject\spiderapi\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "D:\pythonProject\spiderapi\venv\lib\site-packages\django\db\backends\mysql\base.py", line 73, in execute
return self.cursor.execute(query, args)
File "D:\pythonProject\spiderapi\venv\lib\site-packages\MySQLdb\cursors.py", line 206, in execute
res = self._query(query)
File "D:\pythonProject\spiderapi\venv\lib\site-packages\MySQLdb\cursors.py", line 319, in _query
db.query(q)
File "D:\pythonProject\spiderapi\venv\lib\site-packages\MySQLdb\connections.py", line 259, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.OperationalError: (1170, "BLOB/TEXT column 'value' used in key specification without a key length")
错误原因
TextField类型在django中是不需要设置长度限制的,但是如果这个text类型被你设为了unique=True或者在unique_together中作为联合索引使用的话,那就会出现这个错误。
这个错误是mysql数据库出的,和django其实没啥关系。
详细步骤
1、修改model
有两种方式
- 将这个字段从unique或者unqiue_together中删除掉
- 将类型改为CharField类型并设置长度。
2、重新同步数据库
python manage.py makemigrations
python manage.py migrate
3、如果重新同步失败,那是因为上次错误时候的makemigrations文件已经生成
删除最新的一个migrations记录
4、重新同步数据库
python manage.py makemigrations
python manage.py migrate