Django South 迁移无法使用 null=True 和 blank=True。

11

我正在使用Django中的South进行数据库迁移。

在我的models.py文件中,我更改了一个字段,从原来的

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

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

换句话说,我希望使Usergroup字段变为可选。接着,当我尝试运行schemamigration时,South报错了。
(doors)hobbes3@hobbes3:~/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: 

为什么South在抱怨?我没有用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         |         | blah12@gmail.com | ads      | Michael    | Anderson  |       | a      |             | 2012-03-04 06:44:44.97263-05 | 2012-03-04 06:44:44.972661-05
(1 row)

我通过选择选项#3来绕过问题,但我仍然想知道South为什么在抱怨... - hobbes3
只是对你的代码进行评论,括号内除了逗号后最好不要有空格。这并非必须,但符合Python风格标准,更有利于可读性。 - tushar747
@tushar747 是的,这是旧代码。我不再像那样编写了。我已经更新了风格。谢谢! - hobbes3
1个回答

11

我认为...South正在考虑,以防你想要回滚到你还没有进行迁移的时候。

class User(models.Model):
    group = models.ForeignKey(Group)
在这种情况下,如果要回滚,那么默认值是什么?您选择了选项#3,我认为这会禁用回滚到该迁移的能力,从而以这种方式解决您的问题。

嗯,那有点说得通……但我认为只有在将(可以是)空的外键更改回非空的外键时,South 才应该发出警告。此外,你怎么可能为外键设置默认值呢?选择一个随机 id 吗? - hobbes3
1
你说得对。我经常在考虑如何为外键选择默认值时感到困惑。有时候我因此遇到了很多麻烦,甚至不得不重新处理我的数据。我通常会先在外键表中创建一个项目,然后在需要的情况下运行我的迁移并设置默认值。但我相信这不是最好的方法。 - darren

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接