WebFaction
Community site: login faq

i think this example shows it all: after changing my model, i use manage.py sql to rebuild the schema, and then manage.py syncdb to sync it with postgres. but psql does not reflect the changes?! watch in particular the NULL status of attributes like CTYPE:

$ python manage.py sql showCrime

BEGIN;
CREATE TABLE "showCrime_oakcrime" (
    "idx" integer NOT NULL PRIMARY KEY,
    "opd_rd" varchar(10) NOT NULL,
    "oidx" integer NOT NULL,
    "cdate" date NOT NULL,
    "ctime" time NOT NULL,
    "ctype" varchar(50),
    "desc" varchar(80),
    "beat" varchar(5),
    "addr" varchar(80),
    "lat" double precision,
    "long" double precision,
    "ucr" varchar(5),
    "statute" varchar(50),
    "crimeCat" varchar(50)
)
;
COMMIT;

$ python manage.py syncdb

Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

$ psql -U USERNAME djangodb

djangodb=> d "showCrime_oakcrime"

       Table "public.showCrime_oakcrime"
  Column  |          Type          | Modifiers 
----------+------------------------+-----------
 idx      | integer                | not null
 opd_rd   | character varying(10)  | not null
 oidx     | integer                | not null
 cdate    | date                   | not null
 ctime    | time without time zone | not null
 ctype    | character varying(50)  | not null
 desc     | character varying(80)  | not null
 beat     | character varying(5)   | not null
 addr     | character varying(80)  | 
 lat      | double precision       | 
 long     | double precision       | 
 ucr      | character varying(5)   | 
 statute  | character varying(50)  | 
 crimeCat | character varying(50)  | 
Indexes:
    "showCrime_oakcrime_pkey1" PRIMARY KEY, btree (idx)

at one point CTYPE and the others did have NOT NULL qualifiers (because i'd mis-specified blank,null values in my django model) but they are staying as they were; there seems to be some database state maintained somewhere?

asked 21 May '13, 17:02

rikEA
2326
accept rate: 0%


sql doesn't rebuild your schema - it only prints out SQL for the schema that you have defined in your models..

syncdb doesn't alter existing tables.

If you're changing a model for which the table already exists, then you can either:

  • Use "manage.py sql" to display the SQL for the models as it is defined in your code, and compare that to the structure of your live data, and manually run whatever ALTER TABLE commands you need to get your DB caught up with your code.

or

  • Use a migration tool like South to migrate your schemas.

or

  • (if you have no existing data) drop your tables so that Django will re-create them with the new schema the next time you run syncdb.

Hope that helps!

permanent link

answered 21 May '13, 17:38

seanf
12.2k41836
accept rate: 37%

edited 21 May '13, 18:19

"syncdb doesn't alter existing tables." that's what i'd missed; i guess i've always been lucky about earlier versions not being there. +1 SeanF!

(21 May '13, 18:44) rikEA
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×909
×22
×7

question asked: 21 May '13, 17:02

question was seen: 4,832 times

last updated: 21 May '13, 18:44

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM