﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
27816	Duplicate keyword '_binary' failure when using BinaryField with mysql backend	Ace Han	nobody	"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."	Bug	closed	Database layer (models, ORM)	1.10	Normal	invalid	BinaryField mysql		Unreviewed	0	0	0	0	0	0
