Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#22851 closed Bug (fixed)

Default value for BinaryField

Reported by: wkschwartz@… Owned by: Andrew Godwin
Component: Migrations Version: dev
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

Description

django.db.backends.schema.BaseDatabaseSchemaEditor.effective_default (https://github.com/django/django/blob/1.7b4/django/db/backends/schema.py#L157) 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/models.py read as follows:
    from django.db import models
    class M(models.Model):
        b = models.BinaryField(blank=True, editable=False)
  1. Add a to settings.py's INSTALLED_APPS
  2. ./manage.py makemigrations a
  3. ./manage.py migrate

You should see the following error:

  Applying bt.0001_initial...Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "venv3/lib/python3.4/site-packages/django/core/management/__init__.py", line 427, in execute_from_command_line
    utility.execute()
  File "venv3/lib/python3.4/site-packages/django/core/management/__init__.py", line 419, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "venv3/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "venv3/lib/python3.4/site-packages/django/core/management/base.py", line 337, in execute
    output = self.handle(*args, **options)
  File "venv3/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 146, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "venv3/lib/python3.4/site-packages/django/db/migrations/executor.py", line 62, in migrate
    self.apply_migration(migration, fake=fake)
  File "venv3/lib/python3.4/site-packages/django/db/migrations/executor.py", line 96, in apply_migration
    migration.apply(project_state, schema_editor)
  File "venv3/lib/python3.4/site-packages/django/db/migrations/migration.py", 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/models.py", line 30, in database_forwards
    schema_editor.create_model(model)
  File "venv3/lib/python3.4/site-packages/django/db/backends/schema.py", line 197, in create_model
    definition, extra_params = self.column_sql(model, field)
  File "venv3/lib/python3.4/site-packages/django/db/backends/schema.py", line 120, in column_sql
    default_value = self.effective_default(field)
  File "venv3/lib/python3.4/site-packages/django/db/backends/schema.py", 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/__init__.py", line 625, in get_db_prep_save
    prepared=False)
  File "venv3/lib/python3.4/site-packages/django/db/models/fields/__init__.py", 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 by Baptiste Mispelon, 10 years ago

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

Hi,

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 by Tim Graham, 10 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Andrew Godwin, 10 years ago

Owner: changed from nobody to Andrew Godwin
Status: newassigned

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

Resolution: fixed
Status: assignedclosed

In b22917bd5062ad39ad9f00b121a4f6c6fa727a31:

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

comment:4 by Andrew Godwin <andrew@…>, 10 years ago

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