5

Trying to see the SQL that syncdb would generate at the current moment in time.

After several searches the answer wasn't readily apparent -- I know you can use:

python manage.py syncdb --sqlall

returns:

Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created.

How can I output the changes that have happened for the entire database if the code has changed at all?

Is there a way to generate all of the SQL for all of the apps that need syncdb'ing at this time? Or need I just explicitly state each app? I'm not looking for all of the SQL for the entire site, just for the changes that would be implemented by a syncdb.

I've got several apps that need sql generated to describe the changes. I could explicitly list them, but is there a way for syncdb to figure this out for me ?

Wade Williams
  • 3,943
  • 1
  • 26
  • 35

2 Answers2

6

You can do

./manage.py sqlall <app_name>

to get the sql statements and initial data for the app.

If you want just the sql statements,

./manage.py sql <app_name>

Here is a mangement command that prints sqlall for ALL installed apps. Alternatively, you can write your own management command which gets all the installed apps, and calls the ./manage.py sql <app_name> for each.

shuttle87
  • 15,466
  • 11
  • 77
  • 106
karthikr
  • 97,368
  • 26
  • 197
  • 188
  • Thanks kathikr -- But Is there a way to generate all the statements for all the apps that are available now ? (as I click on your link) – Wade Williams Jun 10 '13 at 00:06
  • 1
    You can specify multiple apps (space separated) in the command line. But i dont believe there is one place where you can print "every" app installed. – karthikr Jun 10 '13 at 00:07
  • Would be very interested to know if ther IS a way to produce all the statements for all apps - that's the real point of the question. Will revise. Thanks for your help. – Wade Williams Jun 10 '13 at 00:09
  • Thanks again -- I'm explicitly looking for the sql that'd be generated by syncdb though at this moment though -- not all of the sql for the entire site – Wade Williams Jun 10 '13 at 00:12
  • This is how syncdb is implemented: https://github.com/django/django/blob/master/django/core/management/commands/syncdb.py look for code that deals with `Creating table %s` – karthikr Jun 10 '13 at 00:23
2

The Django Extensions package has a number of custom management commands for django, one of these commands is sqldiff:

https://github.com/django-extensions/django-extensions/blob/master/docs/sqldiff.rst

First,

sudo pip install django-extensions

Next, add django-extensions to installed apps

INSTALLED_APPS = (
    ...
    'django_extensions',
    ...
)

Then, you can

python manage.py sqldiff -a

You'll be presented with a full list of differences, as well as a long list of ALTER TABLE statements that will ensure all fields are set properly (length, null, unsigned, etc)

Any tables that are not created will be listed, and you can then dump the SQL to create them using

python manage.py sqlall {app_label}

It's worth noting that for column name changes, sqldiff will attempt to simply drop renamed columns and create new ones in their place - So don't just copy the entire sqldiff and run it against production without inspecting.

Wade Williams
  • 3,943
  • 1
  • 26
  • 35