Opened 3 hours ago

Closed 5 minutes ago

Last modified 3 minutes ago

#36101 closed New feature (wontfix)

Support BIT data type model field for MySQL and PostgreSQL

Reported by: Jordan Bae Owned by:
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Jordan Bae Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Jordan Bae)

Support BIT data type model field for MySQL and PostgreSQL

Currently, Django's model fields do not support the BIT data type which is available in both MySQL and PostgreSQL databases.

Key reasons for adding BIT data type support:

  1. Improved Database Inspection
    • When using inspectdb on tables containing BIT columns, Django currently falls back to TextField with a "This field type is a guess" comment
    • Native BitField support would provide accurate model generation for existing databases
  1. Database-specific Features
    • MySQL: Support for BIT(M) where M can specify the number of bits
    • PostgreSQL: Support for both BIT(n) and BIT VARYING(n) types


  1. Use Cases
    • Efficient storage of boolean flags and bit flags
    • Direct mapping to database-native bit operations
    • Better integration with legacy databases using BIT columns

This enhancement would improve Django's database type coverage and provide more efficient handling of bit-based data.

Change History (2)

comment:1 by Jordan Bae, 3 hours ago

Description: modified (diff)
Summary: Support bit data type model field on MySQL, PostgreSQLSupport BIT data type model field for MySQL and PostgreSQL

comment:2 by Simon Charette, 5 minutes ago

Resolution: wontfix
Status: newclosed

I'm not convinced that we should add a dedicated field for this given we already have the all purposed BinaryField which I don't see mentioned in your report implemented as longblob on MySQL and bytea on Postgres which should cover most of the usage a BitField would have.

Improved Database Inspection

This argument could stand for pretty much any type that database support and Django doesn't have a field equivalent for.

Efficient storage of boolean flags and bit flags

Apparently bytea is more space efficient than bit varying on PostgreSQL so if you've reached a point where the storage of boolean bits and flags is of a concern BinaryField would be a better option on Postgres at least.

Direct mapping to database-native bit operations

These database-native bit operations would need to be implemented as lookups and transforms anyway on the ORM side which is something that could be done with BinaryField as well.

Given that you can likely implement a BitField by subclassing BinaryField, overriding it's db_type method to return f"bit({self.max_length})" and that adding a BitField in core while there are third-party alternatives would incur a significant maintenance burden I'm going to close this ticket as wontfix.

If you disagree please create a forum discussion to try to form community consensus on the subject as documented when proposing new features.

Last edited 3 minutes ago by Simon Charette (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top