2012-03-04 55 views
10

我正在使用South與Django進行數據庫遷移。Django South遷移不適用於null = True且空白= True

在我models.py我改變的一個字段從

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

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

換句話說,我想使group領域User可選。

然後,當我試圖運行一個schemamigration,南給了我這個錯誤

(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: 

爲什麼南抱怨?我沒有在null = Trueblank = True中指定NULL的默認值嗎?

如果它的事項,這是什麼我現在的表看起來像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 

而且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

我通過選擇選項#3繞過了這個問題,但我仍然想知道爲什麼南方抱怨... – hobbes3 2012-03-04 18:39:54

+0

只需對您的代碼發表評論,除逗號之外,在括號內有空格並不好。不是必需的,但它符合Python風格標準,並且更易於閱讀。 – tushar747 2014-01-06 14:15:43

+0

@ tushar747是的,這是舊代碼。我不再那樣寫了。我更新了風格。謝謝! – hobbes3 2014-01-07 01:06:47

回答

11

我想......南都的情況下,想你想回滾你的遷移到你有的時候

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

在這種情況下,如果回滾它,默認值必須是什麼。您選擇了選項#3,我相信這會禁用回滾到該遷移的能力,從而以這種方式解決您的問題。

+0

嗯那種有道理......但我認爲如果我將(可以是)NULL外鍵更改回NOT NULL外鍵,South應該只會發出警告。除此之外,你認爲無論如何都有一個外鍵的默認值?選擇一個隨機ID? – hobbes3 2012-03-05 01:33:13

+1

你是對的。我經常想知道如何爲FK選擇默認值。我遇到了很多麻煩,有時不得不重做我的數據。我首先在FK表中創建了一個項目,然後如果需要的話,我使用默認值運行我的遷移。我相信這不是最好的方式。 – darren 2012-03-05 08:42:34

相關問題