Opened 5 years ago

Closed 5 years ago

#31036 closed Bug (worksforme)

Primary key has incorrect default on MySQL.

Reported by: Peter Venable Owned by: nobody
Component: Database layer (models, ORM) Version: 2.2
Severity: Normal Keywords: mysql
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Peter Venable)

The problem:

Given a MySQL 5.6 table with only one column, an auto-increment primary key integer, inserting a row should be as simple as calling MyTable.objects.create()
However, the generated SQL is:

INSERT INTO `myapp_mytable` (`id`) VALUES (DEFAULT);
-- Error Code: 1062. Duplicate entry '0' for key 'PRIMARY'

Instead it should generate:

INSERT INTO `myapp_mytable` (`id`) VALUES (NULL);

The fix could be a one-line edit to https://github.com/django/django/blob/master/django/db/backends/base/operations.py#L294 changing 'DEFAULT' to 'NULL'.

Change History (2)

comment:1 by Peter Venable, 5 years ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 5 years ago

Resolution: worksforme
Status: newclosed
Summary: MySQL driver has incorrect primary key default, causing failure to insert auto increment rows with no additional values.Primary key has incorrect default on MySQL.

DEFAULT works completely fine for AUTO_INCREMENT columns on MySQL. I'm not able to reproduce your issue.

Note: See TracTickets for help on using tickets.
Back to Top