2013-04-09 97 views
1

我有一个在数据库中的现有模型。我想用hstore字段来增强它。我安装hstore Postgres的扩展,Django的hstore应用,在Django项目改变了适当的设置:如何在添加hstore字段后创建迁移? (django-hstore与南)

SOUTH_DATABASE_ADAPTERS = {'default': 'south.db.postgresql_psycopg2'} 
DATABASES = { 
    'default': { 
     'ENGINE': 'django_hstore.postgresql_psycopg2', 
     ... 

我检查了Django应用程序的工作原理与新设置 - 它。所以我添加了新的字段到其中一个模型:

data = hstore.DictionaryField(db_index=True) 

下一步:数据库迁移。在这里我迷路了。当试图为新字段创建迁移时,我得到:

The field 'Project.data' does not have a default specified, yet is NOT NULL. 
Since you are adding this field, 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 

我该怎么做?我错过了什么?在任何django-hstore相关文章中,我没有找到任何对默认值(或null = True)的引用。

回答

1

当South试图更新数据库上的模型并且发现表上的现有行试图修改时,通常会显示此消息。为了继续并在数据库上创建新的字段,您必须为正在迁移的tha表的现有行指定一个值。我通常会做的,如果它是一个开发阶段,我会选择编号2并将值设置为0,{}空字典,甚至NULL,具体取决于字段类型。

+0

呀,我试着用{}最初,这申请移民时破产。恐怕hstore.DictionaryField是一个完全不同的野兽。 – Mike 2013-04-09 18:41:28

+0

问题可能是索引标志,我认为数据库将字典作为索引处理很复杂。 – PepperoniPizza 2013-04-09 19:55:46

+0

实际上起初我没有尝试过,原来它是hstore DictionaryField的默认设置。 – Mike 2013-04-09 20:12:06

1

正如已经mentionned,当你打2,你可以去一个空字符串(“”)或填写领域它的存储方式:

? The field 'MyModel.data' does not have a default specified, yet is NOT NULL. 
? Since you are adding this field, 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 
? Please select a choice: 2 
? Please enter Python code for your one-off default value. 
? The datetime module is available, so you can do e.g. datetime.date.today() 
>>> "foo=>bar,foo2=>bar2"