40093

Unknown column '' in 'field list'. Django

Question:

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 <a href="https://docs.djangoproject.com/en/1.9/intro/" rel="nofollow">Django getting started tutorial</a>.

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:

<blockquote>

Unknown column 'polls_choice.votes' in 'field list

</blockquote> 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'")

Answer1:

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.

Recommend

  • Why does return value wrap around 256 in c [duplicate]
  • CSS - Image on right side of page with text not wrapping around it
  • int vs float arithmetic efficiency in Java
  • MPMoviePlayerController - Duration always 0
  • Arrow is not drawn correctly on fabricJS canvas
  • C++ - A 3-d data structure - should I use vector of pointers or vector of vectors?
  • Exception in thread “main” java.awt.AWTError: Assistive Technology not found: com.sun.java.accessibi
  • Properly handle WM_PASTE in subclass procedure
  • Shared Forms Auth cookie not working on subdomain
  • Eloquent Javascript: Can't understand how the number value is determined in the sum function
  • How to populate a Mutation for a different Cassandra table in a trigger
  • How to work with Mailgun API in CodeIgniter; Forbidden error in curl_exe()
  • get value using jquery
  • CameraPreviewImageSource empty preview frame
  • Set selected using jQuery
  • How to make responsive images in flutter?
  • Target in barchart in dc.js
  • Can I switch the 'connected' user within an sql script that is sourced by mysql?
  • Arc gradients in Flutter?
  • Gitlab: copy project to other git lab repository
  • How to highlighting active menu item on scroll and click?
  • Java 11 and E(fx)clipse JavaFX plugin on Eclipse 4.9: An error has occurred - see the log file
  • android : speech recognition what are the technologies available
  • $this->a->b->c->d calling methods from a superclass in php
  • Homomorphic filtering in OpenCV
  • PHP: Need to close STDIN in order to read STDOUT?
  • flex tree gets chopped even after using scroll bar
  • What is the difference between dynamically creating a script tag and statically embed a script tag?
  • Regex not working in java 1.5
  • Content-Type alternative in MQTT
  • How to turn off notice reporting in xampp?
  • how to run ejabberd with Erlang on Heroku?
  • Using redis as an LRU cache for postgres
  • Bad automatic Triangulation with Mayavi for coloring a surface known only by its corner
  • Call Microservice from another Microservice within Docker
  • Sign a Pdf using custom digital signature in Java