开发者

DatabaseError: no such column error

开发者 https://www.devze.com 2023-04-03 05:23 出处:网络
So I have a model that I wanted to add ImageField to, so I typed in picture = models.ImageField(upload_to=\'media/images\')

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.


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.


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.


As of 1.7 migrations within Django replaces South.

Create a new set of migration instructions by running the following command in terminal:

$ python manage.py makemigrations

Check the output in the migration folder it creates to make sure they make sense then run the following terminal command to complete the migrations:

$ python manage.py migrate

That's it.

Running migrations this way allows others to implement the same migrations instead of having to manually implement db changes on every machine using the code. On the new machine all they have to run is:

$ python manage.py migrate
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号