2013-07-21 68 views
2

在Postgres 9.1上使用Django 1.3。Django 1.3 - BooleanField到DateTimeField遷移失敗

我的任務是將兩個舊布爾字段pulledmail_report遷移到時間戳。

在試圖遷移時,我得到以下錯誤,我不確定如何在數據庫中手動刪除非空約束以避免讓我將所有記錄強制轉換爲空。 django.db.utils.DatabaseError: column "pulled" cannot be cast to type timestamp with time zone

任何有助於解決這個問題的方法都不會讓我手動修補我們的實時數據庫,我們將不勝感激。

模型聲明的變化:

 # Reporting Checked Flags 
    # pulled => Object has been processed through order_picklist 
- pulled = models.BooleanField(default=False) 
+ pulled = models.DateTimeField(blank=True, null=True, default=None) 
    # mail_report => Object has been processed through report_mailing_list 
- mail_report = models.BooleanField(default=False) 
+ mail_report = models.DateTimeField(blank=True, null=True, default=None) 
+0

我認爲最好在兩次分離的遷移中做到這一點:首先,從模型中刪除兩列,然後用新類型重新創建它們。 –

+0

@PauloBu我想我唯一的麻煩是我需要保存現有布爾的狀態,所以我可以添加一個datetime.now()標記爲所有當前標記爲true的標記。 – DivinusVox

回答

1

你應該閱讀the Data Migration section in the South's docs。在那裏,他們教你如何將明文密碼更改爲散列密碼。要做到這一點,他們需要保留當前密碼的價值並將其轉換。

我的建議是去,逐步與遷移喜歡這樣的:

  1. 與名稱創建新datetime專欄:pulled2mail_report2
  2. 創建一個數據遷移來填充取決於布爾此列在他們respectives列pulledmail_report
  3. 值創建另一個遷移刪除布爾列pulledmail_report
  4. 創建最後移植到重命名pulled2mail_report2pulledmail_report

希望這個綱要和the tutorials在文檔有所幫助!

+0

這將工作得很好,我用這個概念來得到我需要的東西。不幸的是,我們必須預先安裝這些事務,以使我們的在線服務器儘可能少,這意味着我無法花時間遷移,更改源,遷移。所以我使用原始查詢來避免Django的查詢參數驗證,以獲取id列表,刪除舊列表,創建一個新列表,並迭代id列表以初始化它們。感謝讓我以正確的方向思考。 – DivinusVox

+0

好吧,我很高興你可以反正:) –