2012-03-04 17 views
10

Używam Django do migracji bazy danych.Migracja Django South nie działa z pustym = True i puste = True

w moim models.py zmieniłem jedno z pól od

class User(models.Model): 
    group = models.ForeignKey(Group) 

do

class User(models.Model): 
    group = models.ForeignKey(Group, null = True, blank = True) 

Innymi słowy, chciałem zrobić pole group dla User opcjonalnym.

Wtedy gdy próbowałem uruchomić schemamigration, South dał mi ten błąd

(doors)[email protected]:~/Sites/mysite$ python manage.py schemamigration doors --auto 
? The field 'User.group' does not have a default specified, yet is NOT NULL. 
? Since you are making this field nullable, you MUST specify a default 
? value to use for existing rows. Would you like to: 
? 1. Quit now, and add a default to the field in models.py 
? 2. Specify a one-off value to use for existing columns now 
? 3. Disable the backwards migration by raising an exception. 
? Please select a choice: 

Dlaczego Południowa skarży? Czy nie określiłem wartości domyślnej NULL z null = True i blank = True?

W przypadku nie ma znaczenia to, co moja aktualna tabela wygląda na doors_user

mysite=# \d doors_user 
            Table "public.doors_user" 
    Column |   Type   |      Modifiers       
-------------+--------------------------+--------------------------------------------------------- 
id   | integer     | not null default nextval('doors_user_id_seq'::regclass) 
group_id | integer     | not null 
user_type | character varying(1)  | not null default 't'::character varying 
comment  | text      | not null 
email  | character varying(75) | not null 
password | character varying(135) | not null 
first_name | character varying(135) | not null 
last_name | character varying(135) | not null 
phone  | character varying(135) | not null 
status  | character varying(1)  | not null default 'p'::character varying 
location_id | integer     | 
t_created | timestamp with time zone | not null 
t_modified | timestamp with time zone | not null 
Indexes: 
    "doors_user_pkey" PRIMARY KEY, btree (id) 
    "doors_user_group_id" btree (group_id) 
    "doors_user_location_id" btree (location_id) 
Foreign-key constraints: 
    "group_id_refs_id_2fde5e861cc0e5fe" FOREIGN KEY (group_id) REFERENCES doors_group(id) DEFERRABLE INITIALLY DEFERRED 
    "location_id_refs_id_13c85dcc5cba5e23" FOREIGN KEY (location_id) REFERENCES doors_location(id) DEFERRABLE INITIALLY DEFERRED 
Referenced by: 
    TABLE "doors_property" CONSTRAINT "owner_id_refs_id_7a3a10af3eba8739" FOREIGN KEY (owner_id) REFERENCES doors_user(id) DEFERRABLE INITIALLY DEFERRED 
    TABLE "doors_order" CONSTRAINT "user_action_id_refs_id_79506d7c5228f713" FOREIGN KEY (user_action_id) REFERENCES doors_user(id) DEFERRABLE INITIALLY DEFERRED 
    TABLE "doors_order" CONSTRAINT "user_created_id_refs_id_79506d7c5228f713" FOREIGN KEY (user_created_id) REFERENCES doors_user(id) DEFERRABLE INITIALLY DEFERRED 
    TABLE "doors_log" CONSTRAINT "user_id_refs_id_3ce582a126688737" FOREIGN KEY (user_id) REFERENCES doors_user(id) DEFERRABLE INITIALLY DEFERRED 
    TABLE "doors_ordercomment" CONSTRAINT "user_id_refs_id_6d10d6e79572e14d" FOREIGN KEY (user_id) REFERENCES doors_user(id) DEFERRABLE INITIALLY DEFERRED 

oraz sprawozdania SELECT

mysite=# select * from doors_user; 
id | group_id | user_type | comment |  email  | password | first_name | last_name | phone | status | location_id |   t_created   |   t_modified   
----+----------+-----------+---------+------------------+----------+------------+-----------+-------+--------+-------------+------------------------------+------------------------------- 
    1 |  1 | w   |   | [email protected] | ads  | Michael | Anderson |  | a  |    | 2012-03-04 06:44:44.97263-05 | 2012-03-04 06:44:44.972661-05 
(1 row) 
+0

Usunąłem problem, wybierając opcję # 3, ale nadal chciałbym wiedzieć, dlaczego Południe narzekało ... – hobbes3

+0

Wystarczy komentarz do kodu, nie jest dobrze mieć spacje w nawiasach za wyjątkiem przecinka. Nie jest to konieczne, ale jest w standardach stylu Pythona, a jego lepsza czytelność. – tushar747

+0

@ tushar747 Tak, to stary kod. Nie piszę już tak. Zaktualizowałem styl. Dzięki! – hobbes3

Odpowiedz

11

myślę ... South myśli w razie gdybyś chciał wycofać Twoja migracja do czasu, gdy miałeś

W takim przypadku, jaka byłaby wartość domyślna, gdyby została wycofana. Wybrałeś opcję # 3, która moim zdaniem uniemożliwia przywrócenie migracji, a tym samym naprawienie problemu w ten sposób.

+0

Hmm, to ma sens ... Ale myślę, że South powinien ostrzegać tylko wtedy, gdy zmienię (może być) NULL klucz obcy z powrotem na NIE NULL klucz obcy. Poza tym, skąd w ogóle masz domyślną wartość klucza obcego? Wybierz losowy identyfikator? – hobbes3

+1

Masz rację. Często zastanawiałem się, jak powinienem wybrać domyślny dla FK. Mam wiele problemów, czasami muszę przerobić moje dane z tego powodu. Najpierw utworzyłem element w tabeli FK, a następnie w razie potrzeby uruchomiłem moją migrację z domyślną. Jestem pewien, że to nie jest najlepszy sposób. – darren

Powiązane problemy