Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#22851 closed Bug (fixed)

Default value for BinaryField

Reported by: wkschwartz@… Owned by: Andrew Godwin
Component: Migrations Version: master
Severity: Release blocker Keywords: binaryfield, default
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


django.db.backends.schema.BaseDatabaseSchemaEditor.effective_default ( selects "" as the default value if none is given for a BinaryField. When you try to migrate for the first time, you'll get an error.

Temporary workaround

If you have a BinaryField that you want to have both null=True and editable=False, make sure to give it an explicit default of a bytes value such as b"".

Steps to reproduce

  1. Use Python 3.4.1 and Django 1.7b4.
  2. Make an empty project with a single empty app called a.
  3. Make a/ read as follows:
    from django.db import models
    class M(models.Model):
        b = models.BinaryField(blank=True, editable=False)
  1. Add a to's INSTALLED_APPS
  2. ./ makemigrations a
  3. ./ migrate

You should see the following error:

  Applying bt.0001_initial...Traceback (most recent call last):
  File "./", line 10, in <module>
  File "venv3/lib/python3.4/site-packages/django/core/management/", line 427, in execute_from_command_line
  File "venv3/lib/python3.4/site-packages/django/core/management/", line 419, in execute
  File "venv3/lib/python3.4/site-packages/django/core/management/", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "venv3/lib/python3.4/site-packages/django/core/management/", line 337, in execute
    output = self.handle(*args, **options)
  File "venv3/lib/python3.4/site-packages/django/core/management/commands/", line 146, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "venv3/lib/python3.4/site-packages/django/db/migrations/", line 62, in migrate
    self.apply_migration(migration, fake=fake)
  File "venv3/lib/python3.4/site-packages/django/db/migrations/", line 96, in apply_migration
    migration.apply(project_state, schema_editor)
  File "venv3/lib/python3.4/site-packages/django/db/migrations/", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "venv3/lib/python3.4/site-packages/django/db/migrations/operations/", line 30, in database_forwards
  File "venv3/lib/python3.4/site-packages/django/db/backends/", line 197, in create_model
    definition, extra_params = self.column_sql(model, field)
  File "venv3/lib/python3.4/site-packages/django/db/backends/", line 120, in column_sql
    default_value = self.effective_default(field)
  File "venv3/lib/python3.4/site-packages/django/db/backends/", line 172, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "venv3/lib/python3.4/site-packages/django/db/models/fields/", line 625, in get_db_prep_save
  File "venv3/lib/python3.4/site-packages/django/db/models/fields/", line 2023, in get_db_prep_value
    return connection.Database.Binary(value)
  TypeError: memoryview: str object does not have the buffer interface

Suggested fix

The aforementioned effective_default method should detect BinaryFields and if they allow empty strings, give them a default value of the empty bytes string rather than Unicode string. Specifically, this is probably a problem on Python 3 more so than Python 2.

Change History (5)

comment:1 Changed 5 years ago by Baptiste Mispelon

Component: Python 3Migrations
Severity: NormalRelease blocker
Type: UncategorizedBug
Version: master


I can indeed reproduce the issue.

I'll bump the severity to release blocker as well.

Thanks for this quality bug report (nice format, easy steps to reproduce and a traceback: I wish all our reports were like this one)!

comment:2 Changed 5 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:3 Changed 5 years ago by Andrew Godwin

Owner: changed from nobody to Andrew Godwin
Status: newassigned

comment:5 Changed 5 years ago by Andrew Godwin <andrew@…>

Resolution: fixed
Status: assignedclosed

In b22917bd5062ad39ad9f00b121a4f6c6fa727a31:

Fixed #22851: BinaryView wasn't getting a binary default

comment:4 Changed 5 years ago by Andrew Godwin <andrew@…>

Resolution: fixed
Status: assignedclosed

In 6e7da2bfafb63f5f7c35ed281807cdb54794c2f0:

[1.7.x] Fixed #22851: BinaryView wasn't getting a binary default

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