Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DatabaseError: no such column error

So I have a model that I wanted to add ImageField to, so I typed in picture = models.ImageField(upload_to='media/images')

I then ran syncdb and went into the shell:

python2 manage.py syncdb
python2 manage.py shell

I then imported the model and tried

"model".objects.get(pk=1)

I get the error:

DatabaseError: no such column: people_people.picture

When I run manage.py sql for the model

"picture" varchar(100) NOT NULL

is in the database.

What solutions do you guys have? I can't delete the data in the database.

like image 344
Kevin Avatar asked Sep 06 '11 17:09

Kevin


2 Answers

As noted in the documentation syncdb doesn't add columns to existing tables, it only creates new tables.

I suggest running

python manage.py sqlall <your_app>

Looking at the sql it outputs for the table you're changing, and then running

python manage.py dbshell

in order to manually issue an ALTER TABLE command.

In future, you might like to use a migration tool like South.

like image 156
Duncan Parkes Avatar answered Nov 12 '22 16:11

Duncan Parkes


There are two possibilities that to get this error 1) You added extra field to model after doing the syncdb. 2) you added new class to model.py file in django.

Solution for this is:

First install south by using command

for windows: **easy_install south**     //for that you need to go to the script folder of python folder in c drive.

for linux: **sudo easy_install south**  

Then follow the steps which are included here migration tutorials

step1- python manage.py schemamigration your_app_name --initial

step-2 python manage.py migrate your_app_name Hope this will help you.

like image 3
Wagh Avatar answered Nov 12 '22 16:11

Wagh