#27816: Duplicate keyword '_binary' failure when using BinaryField with mysql
backend
-------------------------------------+-------------------------------------
               Reporter:  Ace Han    |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  1.10
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:  BinaryField mysql
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Although it's bad design in 99% of the cases, my project just need to
 store file in db anyway

 Below is my code
 {{{
 #!div style="font-size: 80%"
 Code highlighting:
   {{{#!python
   # models.py
   class DbBasedFile(models.Model):
     filename = models.CharField(_('filename'), max_length=128)
     content = models.BinaryField(_('content'))
     size = models.PositiveIntegerField(_('size'))

     class Meta:
         unique_together = (('filename', ), )

     def __str__(self):
         return '{}, filename: {}'.format(self.__class__.__name__,
 self.filename)

     if __name__ == '__main__':
         DbBasedFile(filename='1.txt', content=b'abc', size=3).save()
   }}}
 }}}

 will get error
 {{{
 ProgrammingError at /api/misc/dbfiles/file/
 (1064, "You have an error in your SQL syntax; check the manual that
 corresponds to your MySQL server version for the right syntax to use near
 '_binary'abc', 3)'
 }}}

 The pre-generated sql is like below
 {{{
 #!div style="font-size: 80%"
 Code highlighting:
   {{{#!python
   str: INSERT INTO `misc_dbbasedfile` (`filename`, `content`, `size`)
 VALUES (%s, _binary %s, %s)
   }}}
 }}}

 The final sql is like below
 {{{
 #!div style="font-size: 80%"
 Code highlighting:
   {{{#!python
   bytes: b"INSERT INTO `misc_dbbasedfile` (`filename`, `content`, `size`)
 VALUES ('1.txt', _binary _binary'abc', 3)"
   }}}
 }}}

 As we can see, there are two ''''_binary''' in the final sql which will
 fail eventually

 After a little debugging, I found these  '''_binary''' in

 {{{
 
django/django/db/backends/mysql/operations.py.DatabaseOperations.binary_placeholder_sql
 django/db/models/fields/__init__.py.BinaryField.get_placeholder
 }}}

 , which I think is the root cause


 Please kindly help to fix it, thx.

--
Ticket URL: <https://code.djangoproject.com/ticket/27816>
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/050.0547234b6791c7ab5e7a4c2fd0c8e35b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to