Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #515 (again) - renamed "key" field in SQL cache to "cache_key" …

…because

MySQL reserves "key".


git-svn-id: http://code.djangoproject.com/svn/django/trunk@695 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 871ffbe7ee30fae603cbde984ac5975ad5516aa1 1 parent a978dbe
@jacobian jacobian authored
Showing with 11 additions and 10 deletions.
  1. +9 −9 django/core/cache.py
  2. +2 −1  django/core/management.py
View
18 django/core/cache.py
@@ -377,13 +377,13 @@ def __init__(self, table, params):
def get(self, key, default=None):
cursor = db.cursor()
- cursor.execute("SELECT key, value, expires FROM %s WHERE key = %%s" % self._table, [key])
+ cursor.execute("SELECT cache_key, value, expires FROM %s WHERE cache_key = %%s" % self._table, [key])
row = cursor.fetchone()
if row is None:
return default
now = datetime.now()
if row[2] < now:
- cursor.execute("DELETE FROM %s WHERE key = %%s" % self._table, [key])
+ cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key])
db.commit()
return default
return pickle.loads(base64.decodestring(row[1]))
@@ -399,21 +399,21 @@ def set(self, key, value, timeout=None):
if num > self._max_entries:
self._cull(cursor, now)
encoded = base64.encodestring(pickle.dumps(value, 2)).strip()
- cursor.execute("SELECT key FROM %s WHERE key = %%s" % self._table, [key])
+ cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key])
if cursor.fetchone():
- cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE key = %%s" % self._table, [encoded, str(exp), key])
+ cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table, [encoded, str(exp), key])
else:
- cursor.execute("INSERT INTO %s (key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)])
+ cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)])
db.commit()
def delete(self, key):
cursor = db.cursor()
- cursor.execute("DELETE FROM %s WHERE key = %%s" % self._table, [key])
+ cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key])
db.commit()
def has_key(self, key):
cursor = db.cursor()
- cursor.execute("SELECT key FROM %s WHERE key = %%s" % self._table, [key])
+ cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key])
return cursor.fetchone() is not None
def _cull(self, cursor, now):
@@ -424,8 +424,8 @@ def _cull(self, cursor, now):
cursor.execute("SELECT COUNT(*) FROM %s" % self._table)
num = cursor.fetchone()[0]
if num > self._max_entries:
- cursor.execute("SELECT key FROM %s ORDER BY key LIMIT 1 OFFSET %%s" % self._table, [num / self._cull_frequency])
- cursor.execute("DELETE FROM %s WHERE key < %%s" % self._table, [cursor.fetchone()[0]])
+ cursor.execute("SELECT cache_key FROM %s ORDER BY cache_key LIMIT 1 OFFSET %%s" % self._table, [num / self._cull_frequency])
+ cursor.execute("DELETE FROM %s WHERE cache_key < %%s" % self._table, [cursor.fetchone()[0]])
##########################################
# Read settings and load a cache backend #
View
3  django/core/management.py
@@ -626,7 +626,8 @@ def createcachetable(tablename):
"Creates the table needed to use the SQL cache backend"
from django.core import db, meta
fields = (
- meta.CharField(name='key', maxlength=255, unique=True, primary_key=True),
+ # "key" is a reserved word in MySQL, so use "cache_key" instead.
+ meta.CharField(name='cache_key', maxlength=255, unique=True, primary_key=True),
meta.TextField(name='value'),
meta.DateTimeField(name='expires', db_index=True),
)
Please sign in to comment.
Something went wrong with that request. Please try again.