DatabaseError: no such column error - django

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

Related

What is the problem in this shell code (Django and sql)

I got this error in my shell code.
You've to use this, it is because till now you've only declared the table not exactly created in you database, this will create/update table details in the database
python manage.py makemigrations
python manage.py migrate
Then reload the shell and run this command

Adding new field to existing database table in django python

I have a Postgresql database connected with my django application, Now I have a existing table in the database and want to add a new field to this table using the migrate command. But when I try to add this new field to my models.py and run makemigration and migrate commands, django says there are no new migrations to apply. Can you help me with how to add a new field to existing table.
Try running makemigrations command specifying your app name like this:
python manage.py makemigrations myapp
and then try running migrate command
After a long research I got a solution as below:
pip install django-extensions
and add this in the install app of settings.py
INSTALLED_APPS = [
'django_extensions'
]
Now, run migrations for your installed apps
python manage.py makemigrations your_app_name
python manage.py migrtate your_app_name
Done! See Your Database...

Django Migrations command workflow

There are three migration commands in Django:
python manage.py makemigrations
python manage.py migrate
python manage.py syncdb
In what order do these commands should be executed in terms of workflow of a basic project?
I am using Version: 1.8
syncdb is deprecated and does the same as migrate.
Whenever you make a change to your models, and when you first create them, each time you'd want to first run makemigrations to create the migration files, then migrate to apply them to your database.

Can't add South to Django 1.6.2 Project

I've got an existing Django project that I'm trying to add South to. I'm fine with losing ally my data (in fact, I've already dropped/created the database several times).
The problem is that I do the following:
(in psql)
drop database myproject
create database myproject
(at the command line)
python manage.py syncdb --migrate
python manage.py schemamigration myproject.myapp --initial
python manage.py migrate myproject.myapp
Everything works great until I get to that last command; when I run it I get:
django.db.utils.ProgrammingError: relation "myapp_somemodel" already exists
(where "somemodel" is a model in myapp).
I've tried searching SO, but all the posts I found suggested the set of commands above. Can anyone please help me get South added to this project?
You need to run migrate with --fake option for your initial migration:
$ python manage.py migrate myproject.myapp 0001 --fake
Also see Converting An App chapter from South docs.

Django - Deploy syncdb & South

I'm deploying a project on a new development environment.
As I'm using South I did:
$ python manage.py syncdb --all
$ python manage.py migrate --fake
I used syncdb --all to apply actual state of models.
Then migrate --fake to mark all models as migrated.
But after that, my model is not on the last version (missing fields)
What am I doing wrong ?
I assume all my modifications have migrations.
If I do
$ python manage.py syncdb
It seems to create the first state since when I used South (that is expected)
But then
$ python manage.py migrate
Some tables appears as already created
Actually this, should have been fine for my case
$ python manage.py syncdb --all
$ python manage.py migrate --fake
Having to redeploy my app recently, I faced the same issue.
I just realized that I had a double initial migration on the model that were causing the problem
0001_initial.py
0002_initial.py
0003_auto__add_field_mytable_myfield.py
I simply deleted & renamed
0001_initial.py
0002_auto__add_field_mytable_myfield.py
Then redone the whole database deployment (obviously not forgetting to update the already applied migrations on my other hosts)
--fake option does not avoid errors while trying to create new migrations. It records that migrations have applied without actually applying them.
Also, you need --ignore-ghost-migrations or --delete-ghost-migrations to achieve what you are looking for.
To convert an existing project to south, you first need to convert the app
Now, if you have already run --fake, to recover, you can do this:
Go to ./manage.py dbshell
DELETE FROM south_migrationhistory WHERE id > 0; //Note that this would delete everything in the table.
If you want to remove migrations of a specific app,
DELETE FROM south_migrationhistory WHERE app_name = 'blah'