Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

python - Unknown column '' in 'field list'. Django

I'm using Django 1.9 and MySQL. I want to rename a field in my model. Let's look at model Choice from the Django getting started tutorial.

class Choice(models.Model):
  question = models.ForeignKey(Question)
  choice_text = models.CharField(max_length = 200)
  votes = models.IntegerField(default=0)

So, I want to rename the votes field to votes_count. I created an empty migration and add to operations following line:

migrations.RenameField (
    model_name='choice',
    old_name='votes',
    new_name='votes_count',
),

After python manage.py migrate, the field in the database table was renamed. But when I tried to use this model

def vote(request, question_id):
  question = get_object_or_404(Question, pk=question_id);
  try:
    selected = question.choice_set.get(pk=request.POST['choice'])
  except (KeyError, Choice.DoesNotExist):
    return render(request, 'polls/detail.html', {
      'question':question,
      'error_message':"You didn't select a choice."
    })
  else:
    selected.votes_count += 1
    selected.save()
    return HttpResponseRedirect(reverse('polls:results', args=(question_id)))

I got:

Unknown column 'polls_choice.votes' in 'field list

    Traceback:

    File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
      64.                 return self.cursor.execute(sql, params)

    File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/backends/mysql/base.py" in execute
      112.             return self.cursor.execute(query, args)

    File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in execute
      226.             self.errorhandler(self, exc, value)

    File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/connections.py" in defaulterrorhandler
      36.         raise errorvalue

    File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in execute
      217.             res = self._query(query)

    File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in
_query
      378.         rowcount = self._do_query(q)

    File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in
_do_query
      341.         db.query(q)

    File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/connections.py" in query
      280.             _mysql.connection.query(self, query)

    The above exception ((1054, "Unknown column 'polls_choice.votes' in 'field list'")) was the direct cause of the following exception:

    File "/home/verdigo/venv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
      149.                     response = self.process_exception_by_middleware(e, request)

    File "/home/verdigo/venv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
      147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

    File "./polls/views.py" in vote
      23.     selected = question.choice_set.get(pk=request.POST['choice'])

    File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/models/manager.py" in manager_method
      122.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

    File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/models/query.py" in get
      381.         num = len(clone)

    File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/models/query.py" in __len__
      240.         self._fetch_all()

    File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/models/query.py" in _fetch_all
      1074.             self._result_cache = list(self.iterator())

    File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/models/query.py" in __iter__
      52.         results = compiler.execute_sql()

    File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in execute_sql
      848.             cursor.execute(sql, params)

    File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
      79.             return super(CursorDebugWrapper, self).execute(sql, params)

    File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
      64.                 return self.cursor.execute(sql, params)

    File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/utils.py" in
__exit__
      95.                 six.reraise(dj_exc_type, dj_exc_value, traceback)

    File "/home/verdigo/venv/lib/python3.4/site-packages/django/utils/six.py" in reraise
      685.             raise value.with_traceback(tb)

    File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
      64.                 return self.cursor.execute(sql, params)

    File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/backends/mysql/base.py" in execute
      112.             return self.cursor.execute(query, args)

    File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in execute
      226.             self.errorhandler(self, exc, value)

    File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/connections.py" in defaulterrorhandler
      36.         raise errorvalue

    File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in execute
      217.             res = self._query(query)

    File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in
_query
      378.         rowcount = self._do_query(q)

    File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in
_do_query
      341.         db.query(q)

    File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/connections.py" in query
      280.             _mysql.connection.query(self, query)

    Exception Type: OperationalError at /polls/1/vote/
    Exception Value: (1054, "Unknown column 'polls_choice.votes' in 'field list'")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It looks as if you created and ran a migration to rename the model field from votes to votes_count, but did not update the model at the same time.

When the Django tries to fetch the model from the db, it tries to select the votes column because you still have a votes field in your models, and you get the error because the column doesn't exist in the database.

Creating a manual migration isn't normally necessary. Usually, you would rename the model field, run makemigrations, then run migrate. The advantage of letting Django create the migration is that you can be confident that the database is in sync with your models after you have run migrate.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...