Django IntegrityError: no default value

python mysql django django-orm

658 просмотра

2 ответа

There is a longer SQL query that I need to run to migrate data from one table to another. It worked fine all the way up to this week when I migrated to Django 1.9.2 and Python 3.5.

The problem is that the table has a field 'last_update' that is NULL by default. The table definition is

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

I've also checked with MySQL Workbench and the table is indeed set to allow NULLs on that field and the default is NULL.

The query crashes after about 30 minutes with the error message:

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

Very irritating! How can I insert rows to that table?

Per request, here is the table definition:

CREATE TABLE `google_pla_plaproducts` (
  `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`)
Автор: dengar81 Источник Размещён: 08.11.2019 11:07

Ответы (2)

0 плюса

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

this should work. i have used this before and it is working fine for me.

Автор: Rohit Chopra Размещён: 20.08.2016 04:15

0 плюса

I ended up modifying the table a few times. I didn't try the suggestion from Rohit, as the default=None. It started working again, after using multiple DB migrations.

Автор: dengar81 Размещён: 21.08.2016 01:33
Вопросы из категории :