django-enumfield icon indicating copy to clipboard operation
django-enumfield copied to clipboard

Use native enums if the database supports them

Open thedrow opened this issue 9 years ago • 8 comments

All major databases that Django support already supports enums at the database level.

You can also implement enums in SQLite using CHECK.

Is there a good way to hook into south and django migrations framework in order to make this field use their native database type?

thedrow avatar Jan 05 '15 15:01 thedrow

Good idea, it would be interesting to try it out. It could be as simple as overriding db_type on the EnumField-class.

https://docs.djangoproject.com/en/dev/howto/custom-model-fields/#custom-database-types

hannseman avatar Jan 08 '15 09:01 hannseman

In postgres you have to run CREATE TYPE first so a migration is required. On the other databases it should be a matter of overrriding db_type.

thedrow avatar Jan 08 '15 13:01 thedrow

Any progress on this?

AncientSwordRage avatar Feb 09 '15 10:02 AncientSwordRage

I'm gonna try to implement it when I'm in London. The next Django sprint will take place there.

thedrow avatar Feb 09 '15 11:02 thedrow

"2. Changing the member list of ENUM columns is very expensive." http://komlenic.com/244/8-reasons-why-mysqls-enum-data-type-is-evil/

kjagiello avatar Aug 14 '15 11:08 kjagiello

I'm looking at a different path, which would have enum values in a classic lookup table with foreign key instead. The table has auto_id, enum_value, enum_label , and i'm trying to come up with a reasonably safe sync method that updates existing databases with new model code from the enum classes as needed.

django-enumfield is cool as its implemented, but i really want to enforce referential integrity at the db level, and also have my database make sense to someone who has never looked at django/python at all.

If someone has done this already, i'd appreciate a pointer.

kaidokert avatar Sep 01 '15 21:09 kaidokert

Upstream Django issue: #24342: Add EnumField model/form fields

Related implementation: https://github.com/ashleywaite/django-more/tree/master/django_enum

PiDelport avatar Oct 25 '17 14:10 PiDelport

is this a stale issue? looks like the issue is dormant on the upstream django tickets board. it would be nice if this library can support it.

Happy to open a PR if it would be considered

HaddadJoe avatar Feb 25 '23 21:02 HaddadJoe