Opened 8 months ago

Last modified 8 months ago

#27813 new Cleanup/optimization

BinaryField type inconsistent between sqlite3 (bytes) and postgresql (memoryview)

Reported by: Antonio Terceiro Owned by: nobody
Component: Database layer (models, ORM) Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I have an application that has the following model:

class Attachment(models.Model):
    # ...
    data = models.BinaryField(default=None)

under sqlite3:

In [1]: Attachment.objects.last().data
Out[1]: b'Mon Feb  6 16:53:37 UTC 2017\n'

under postgresql:

In [1]: Attachment.objects.last().data
Out[1]: <memory at 0x7f1ae8e93108>

i.e. under sqlite the BinaryField values comes as bytes(), and under postgresql it comes as memoryview()

Change History (3)

comment:1 Changed 8 months ago by Tim Graham

I can confirm the difference, however, I can't say if a change should be made. Do you have a suggestion about what to do?

comment:2 Changed 8 months ago by Antonio Terceiro

I noticed this because I had a test that checked the value of the data field after a given operation, and since I was initially developing against sqlite, I compared the value against a bytes() literal b'...'. to make the test pass against postgresql as well, I had to cast the value in the database to bytes() before the comparison.

I would expect to always get the same type, either bytes or memoryview. on the other hand, everything else other than that one test just worked. the view that allows for that data to be downloaded worked just fine, because bytes and memoryview have a similar enough interface, so maybe this should be just clearly documented.

comment:3 Changed 8 months ago by Tim Graham

Summary: BinaryField is inconsistent between sqlite3 and postgresqlBinaryField type inconsistent between sqlite3 (bytes) and postgresql (memoryview)
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

Django's test suite is also casting to bytes for comparison. Accepting for further investigation.

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