dkgirl
dkgirl

Reputation: 4739

Django sqlite - how to change schema

I have made changes to my model.py in Django and now I want to syncronize these changes. It's fine to delete the tables and re-create them. However, nothing seems to work. I am using sqlite3:

syncdb: only works first time, not with changes

"python manage.py sql my_site", followed by syncdb: I thought this would 'redo' it all, but the table still only has the old columns (or so I assume as I get an error when I try to access the table using my model).

Then I figure that I can access the database directly and delete the tables that way. However, I don't know how to get "in" to the DB where I can execute commands. Typing sqlite3 from the command prompt is not recognized. I also tried "python manage.py sql my_site", but I again get the message that sqlite3 is not recognized.

Suggestions?

Upvotes: 2

Views: 4549

Answers (2)

mrmagooey
mrmagooey

Reputation: 4982

Use Django's built in database management tool:

python manage.py dbshell

And issue the required sql commands. The sql command will only print to stdout what the required sql is to create the current tables (as defined by the current models).

Upvotes: 0

amarillion
amarillion

Reputation: 24917

First you have to install the command line tool for sqlite. On Ubuntu/Debian, you can simply do

sudo apt-get install sqlite3

On windows, you can download it from here: http://www.sqlite.org/download.html. Look for the one that looks like sqlite-shell-win32-xxx.zip.

Use it like this:

> sqlite3 /path/to/your/database
;show some help
.help 
; list all databases
.databases
; clear the contents of a table
DELETE FROM <tablename>;

See also the command line reference: http://www.sqlite.org/sqlite.html and the sqlite SQL reference: http://www.sqlite.org/lang.html.

Using the "ALTER TABLE" sql command, you can also add columns without deleting the entire contents of the table. To do this, compare the output of .schema in sqlite3, and the output of manage.py sql my_site to find out which columns you need to add.

An example:

ALTER TABLE "buildreport_series" ADD COLUMN "parameters" text

Upvotes: 1

Related Questions