django 数据库报错
django用数据库当缓存:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'cache_table', # 数据库表
'OPTIONS': {
'MAX_ENTRIES': 300, # 最大缓存记录的数量(默认300)
'CULL_FREQUENCY': 3, # 缓存到达最大个数之后,剔除缓存个数的比例,即:1/CULL_FREQUENCY(默认3)
}
}
}
创建缓存表
python manage.py createcachetable --traceback
File "E:\work\coding\LottoryMatch\venv\lib\site-packages\django\core\management\commands\createcachetable.py", line 43, in handle
self.create_table(db, cache._table, dry_run)
File "E:\work\coding\LottoryMatch\venv\lib\site-packages\django\core\management\commands\createcachetable.py", line 103, in create_table
(tablename, e))
django.core.management.base.CommandError: Cache table 'cache_table' could not be created.
The error was: (1071, 'Specified key was too long; max key length is 767 bytes').
带引创建表的信息
print(full_statement)
CREATE TABLE `cache_table` (
`cache_key` varchar(255) NOT NULL PRIMARY KEY,
`value` longtext NOT NULL,
`expires` datetime(6) NOT NULL
);
修改varchar 767/4->191
with transaction.atomic(using=database, savepoint=connection.features.can_rollback_ddl):
with connection.cursor() as curs:
try:
print(full_statement)
if '''`cache_key` varchar(255)''' in full_statement:
full_statement = full_statement.replace('`cache_key` varchar(255)', '`cache_key` varchar(191)')
curs.execute(full_statement)
except DatabaseError as e:
raise CommandError(
"Cache table '%s' could not be created.\nThe error was: %s." %
(tablename, e))
for statement in index_output:
curs.execute(statement)

浙公网安备 33010602011771号