2013-04-09 28 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"