2016-08-20 70 views
2

我需要運行更長的SQL查詢以將數據從一個表遷移到另一個表。直到本週,當我遷移到Django 1.9.2和Python 3.5時,它一直運行良好。Django IntegrityError:無默認值

問題是該表的字段'last_update'默認爲NULL。表定義爲

last_update = models.DateTimeField("last updated",null=True, auto_now_add=False, auto_now=True) 

我還與MySQL工作臺檢查,該表確實設置爲允許在該領域空白和默認爲NULL。

與錯誤信息大約30分鐘後,查詢崩潰:

django.db.utils.IntegrityError: (1364, "Field 'last_update' doesn't have a default value") 

非常刺激!我如何將行插入到該表中?

每請求時,這裏是表的定義:

CREATE TABLE `google_pla_plaproducts` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `product_code` varchar(55) CHARACTER SET utf8 NOT NULL, 
    `min_bid` decimal(20,4) NOT NULL, 
    `current_bid` decimal(20,4) NOT NULL, 
    `max_bid` decimal(20,4) NOT NULL, 
    `creation_date` datetime(6), 
    `last_update` datetime(6) DEFAULT NULL, 
    `status_change` datetime(6) DEFAULT NULL, 
    `starting_bid` decimal(20,4) NOT NULL, 
    `adgroup_id` int(11) NOT NULL, 
    `status_id` int(11) NOT NULL, 
    `product_price` decimal(20,4) NOT NULL, 
    PRIMARY KEY (`id`,`product_code`,`adgroup_id`), 
    KEY `google_p_adgroup_id_3b7c9d4ecddd04ba_fk_google_pla_plaadgroup_id` (`adgroup_id`), 
    KEY `google_pla_plaprod_status_id_2f8113a5ef0dd021_fk_globs_status_id` (`status_id`), 
    KEY `idx_prod_code` (`product_code`), 
    CONSTRAINT `google_p_adgroup_id_3b7c9d4ecddd04ba_fk_google_pla_plaadgroup_id` FOREIGN KEY (`adgroup_id`) REFERENCES `google_pla_plaadgroup` (`id`), 
    CONSTRAINT `google_pla_plaprod_status_id_2f8113a5ef0dd021_fk_globs_status_id` FOREIGN KEY (`status_id`) REFERENCES `globs_status` (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=88949 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; 
+0

您可以添加工作臺產生的表的SQL定義嗎? –

+0

還值得注意的是,該表實際上包含該字段的空值! – dengar81

+0

可能會添加'models.DateTimeField(default = None,...)' – JulienD

回答

0

updated = models.DateTimeField(default=None, auto_now=True, auto_now_add=False)

這應該工作。 我以前用過這個,它對我來說工作的很好。

+0

你不需要'auto_now'和'auto_now_add'。從[docs](https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.DateField):「選項auto_now_add,auto_now和default是互斥的。任何這些選項的組合會導致錯誤。「 – joshlsullivan

+0

@Rohit Chopra:不,那不是。它以某種方式再次工作。也許在玩過遷移文件後... – dengar81

0

我最終修改了幾次表。我沒有嘗試Rohit的建議,默認= None。在使用多個數據庫遷移後,它再次開始工作。