#17653: using id = 0 on get_or_create
-------------------------------------+-------------------------------------
     Reporter:  sylvain.lebon@…      |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.3
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by drewbrew):

 I'm not sure if my means of handling the settings is the right option
 here, but this seems to work for me (patch is against 1.9.5, not master):

 {{{#!diff

 diff --git a/django/db/backends/mysql/base.py
 b/django/db/backends/mysql/base.py
 index 03f3857..1595356 100644
 --- a/django/db/backends/mysql/base.py
 +++ b/django/db/backends/mysql/base.py
 @@ -261,6 +261,10 @@ class DatabaseWrapper(BaseDatabaseWrapper):
          return kwargs

      def get_new_connection(self, conn_params):
 +        try:
 +            conn_params.pop('ALLOWS_AUTO_PK_0')
 +        except KeyError:
 +            pass
          conn = Database.connect(**conn_params)
          conn.encoders[SafeText] = conn.encoders[six.text_type]
          conn.encoders[SafeBytes] = conn.encoders[bytes]
 diff --git a/django/db/backends/mysql/features.py
 b/django/db/backends/mysql/features.py
 index c8ea8f7..28cb0ad 100644
 --- a/django/db/backends/mysql/features.py
 +++ b/django/db/backends/mysql/features.py
 @@ -33,6 +33,10 @@ class DatabaseFeatures(BaseDatabaseFeatures):
      supports_column_check_constraints = False
      can_clone_databases = True

 +    def __init__(self, connection):
 +        super(DatabaseFeatures, self).__init__(connection)
 +        self.allows_auto_pk_0 =
 self.connection.settings_dict['OPTIONS'].get('ALLOWS_AUTO_PK_0', False)
 +
      @cached_property
      def _mysql_storage_engine(self):
          "Internal method used in Django tests. Don't rely on this from
 your code"
 diff --git a/django/db/backends/mysql/operations.py
 b/django/db/backends/mysql/operations.py
 index 55a5e92..f667614 100644
 --- a/django/db/backends/mysql/operations.py
 +++ b/django/db/backends/mysql/operations.py
 @@ -132,7 +132,7 @@ class DatabaseOperations(BaseDatabaseOperations):

      def validate_autopk_value(self, value):
          # MySQLism: zero in AUTO_INCREMENT field does not work. Refs
 #17653.
 -        if value == 0:
 +        if value == 0 and not self.connection.features.allows_auto_pk_0:
              raise ValueError('The database backend does not accept 0 as a
 '
                               'value for AutoField.')
          return value
 }}}

 Also on GitHub: https://github.com/drewbrew/django/tree/ticket-17653

--
Ticket URL: <https://code.djangoproject.com/ticket/17653#comment:29>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/090.15266ac876124f9dad948fb6de804b65%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to