Adding new field to existing database table in django python - django

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...

Related

django error OperationalError at /admin/blog/post/

i get this error in simple django programme run..
OperationalError at /admin/blog/post/
no such table: blog_post
if django version >=django 1.7
python manage.py makemigrations
python manage.py migrate
else
python manage.py schemamigrations
python manage.py migrate
so you need to migrate (map the model changes / new models into database tables) the changes so that your page works properly
delete the migrations folder in app and then do :
python manage.py makemigrations
python manage.py migrate
python manage.py migrate --run-syncdb
it worked for me
You need to run migrations before you can do that operation. The necessary tables don't exist in your database (as described by the error).
Check for a typo in the name of your Model. If you change your model name after makemigration and migrate, then it doesn't work and find the correct model name anymore.

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.

Setting up an existing Django + South project instance

I've been working with django + south for a while and still haven't nailed this issue down.
Take an existing project, with existing apps and existing migrations that have been added over time. Now suppose you want to deploy it to a new dev machine (for example) with a clean database.
What would be the process?
Remember the that settings at this point contains:
INSTALLED_APPS = (
'django.contrib.auth',
# ...
'south',
'myapp1',
'myapp2',
)
So on one hand, if you try to run the migrations, you won't get anything since no database exists yet. But if you try to syncdb it'll simply sync without the south migrations.
So what's the right process to do this?
How about syncdb, and then migrate?
$ python manage.py syncdb
$ python manage.py migrate
South's patched syncdb management command tells you as much at the end:
$ python manage.py syncdb
Syncing...
Creating tables ...
Installing custom SQL ...
Installing indexes ...
No fixtures found.
Synced:
> django.contrib.auth
Not synced (use migrations):
- myapp1
- myapp2
(use ./manage.py migrate to migrate these)

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.
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

Adding South to Django project, development & production

Adding South to an existing Django project. I have it installed on both the development machine and the "production" server.
I've done the following on the development machine, then: added South app to settings.py,
python manage.py syncdb
python manage.py convert_to_south myproject.myapp
then changed some models, then
python manage.py schemamigration myproject.myapp --auto
python manage.py migrate myproject.myapp
Seems to work so far. What I am now not so sure about is what to do on the production server. Just repeat all these steps manually? Upload modified settings.py, do syncdb, convert_to_south, upload modified models.py, do schemamigration, migrate? Something different? The tutorial here says something about adding migrations to the version control, so, presumably, they should be uploaded and somehow applied on the production server?
Furthermore, right now I am using sqlite3 on the development machine and mysql on the server - does it make things any different south-wise?
My guide says:
Install South on server. import south from shell just to make sure you are using the same python env.
Add 'south' to INSTALLED_APPS in settings.py.
Upload settings.py.
Restart server
python manage.py syncdb.
Upload new app/models.py and app/migrations/ dir.
Restart server.
python manage.py migrate app --fake 0001
python manage.py migrate app
To make sure the south migration table exists,
python manage.py syncdb
and then
python manage.py migrate myproject.myapp --fake 0001
python manage.py migrate myproject.myapp
That's what's worked for me. :)
No need to do this in Django >= 1.7
i am stuck on this more then 1 hour :)
and at last find 1.7 and more have in build upgrading-from-south
for more info https://docs.djangoproject.com/en/1.7/topics/migrations/#upgrading-from-south
may be this one help you