问题 Django South迁移不能使用null = True和blank = True


我在用  使用Django进行数据库迁移。

在我的 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时,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: 

为什么南方抱怨?我没有指定NULL的默认值 null = True 和 blank = True

如果重要,这就是我目前的表格 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)

8505
2018-03-04 18:20


起源

我通过选择选项#3绕过了问题,但我仍然想知道为什么南方抱怨... - hobbes3
只是对你的代码发表评论,除了逗号后,在括号中加上空格并不好。没有必要,但它符合python风格标准,并且更易于阅读。 - tushar747
@ tushar747是的,这是旧代码。我不再这样写了。我已经更新了风格。谢谢! - hobbes3


答案:


我想......南方正在考虑以防你想要将你的迁移回滚到你的时候

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

在这种情况下,如果它被回滚,那么默认值必须是什么。您选择了#3选项,我相信它会禁用回滚到该迁移的功能,从而以这种方式修复您的问题。


11
2018-03-04 20:08



嗯那种有意义...但我认为南方应该警告我是否将(可以)NULL外键改回NOT NULL外键。除了你如何设置外键的默认值?选择随机ID? - hobbes3
你是对的。我经常想知道如何选择FK的默认值。我遇到了很多麻烦,因此有时候不得不重做我的数据。我首先在FK表中创建了一个项目,然后在需要时使用默认值运行我的迁移。我敢肯定这不是最好的方式。 - darren