[Fixed]-Unknown column '' in 'field list'. Django

14👍

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.

2👍

Cause: This error occurs when a new one-to-many foreign key is created, pointing to the model with the forkey field and then generating the table

.

Solution: This problem can be solved by directly deleting the database and rebuilding it.

1: drop database database table;

2, re-create after deletion, before the code created, run directly

1👍

This is an issue that has persisted with me and lead me down a lot of rabbit holes dropping tables etc. A simple solution I have found is answering “N” when django asks you if you are renaming a field of that model (when running makemigrations). What that then essentially performs is a deletion of your previous field and creates the new field. Be careful as you may lose data on existing field so this works with fields which are either new or relatively easy to ‘refill’ their data required. You may need to run –fake if you get an error with regards to not being able to ‘drop field’ when migrating after makemigrations.

Update:
I did the above for a Boolean field and my data was kept. Even though I said N, it seems as if it is essentially a renaming.

👤Josh

0👍

On my end it occured when I added a new column with a oneToMany relationship and the migration failed to pick it up.
To avoid deleting data and recreating the models, Download mysql workbench for your platform. The run the following code to insert the missing column.

use <database_name>;
show tables;
desc <table_name>;
alter table <table_name> add column (< missing field> varchar(255) )

That added the missing colum and the error went away

Leave a comment