Update django database to reflect changes in existing models

I already defined the model and created the database associated with it through manager.py syncdb . Now that I have added several fields to the model, I tried syncdb again, but the output does not appear. When I try to access these new fields from my templates, I get a “No Such Column” exception, which makes me think that syncdb is not actually updating the database. What is the right team here?

+66
python django django-models
Dec 31 '09 at 13:23
source share
8 answers

It seems that you need a migration system. South is really nice, works great, there are some automation tools that make your workflow easier. And has an excellent tutorial .




note: syncdb cannot update existing tables. Sometimes it’s impossible to decide what to do automatically - why the southern scripts are great.

+33
Dec 31 '09 at 13:28
source share

Like Django 1.7+, built-in Django 1.5 )

If your changes violate your old scheme, this will not work - in this case tools like South or Django Evolution are great.

+136
Jan 02 '09 at 19:12
source share

Starting with Django 1.7, you can do this with your own migrations. Just run

 python manage.py makemigrations <your app name> python manage.py migrate 
+33
Dec 27 '14 at 6:17
source share

Django syncdb does not modify existing tables in the database, so you need to do this manually. As I always do this:

  • Change the model class first.
  • Then run: manage.py sql myapp.
  • Look at the sql it prints out and see how it represents the change you are going to make.
  • Make changes manually using the database manager.
  • Make sure everything is working correctly using the admin site.

If you use sqllite, the firefox plugin is a good manager: link

+8
Dec 31 '09 at 14:12
source share

Another tool will be the evolution of django. In most cases, a table reduction is not required.

django evolution

Just install it like any other django application and run:

python manage.py evolve --hint --execute

+8
Jan 01 '09 at 20:24
source share

deseb is a great tool for this.

After installation, you can write. /manage.py sqlevolve and create the sql commands necessary to synchronize the database structure with your models.

+3
Dec 31 '09 at 13:28
source share

You need to abandon your tables before you can recreate them with syncdb .

If you want to keep existing data, you need to unload your database, release your tables, run syncdb to create a new database, and then reload the old data into your new tables.

There are tools that help with this. However, in many cases, this is also easy to do manually.

+2
Dec 31 '09 at 13:30
source share

For versions 1.4.1 and higher, the command has changed to

 python manage.py flush 

Read the white paper before using it as it will delete all your data .

+1
Dec 09 '13 at 1:26
source share



All Articles