-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added enum support #392
base: orm
Are you sure you want to change the base?
added enum support #392
Conversation
Enums should also support ints and floats since those can be used too, so it would be recommended to add dynamic support for those. |
His enums in unit tests are using integers, so they get stored as string and converted back afterwards? class Result(Enum):
SUCCESS = 0
FAILURE = 1
UNKNOWN = 2 Would it store |
I actually was hoping something like class Language(Enum):
EN = 0
DE = 1
ES = 4 would result in something like: DROP TABLE IF EXISTS Language;
CRATE TABLE Language
id INTEGER PRIMARY KEY,
value TEXT NOT NULL
;
INSERT INTO Language (
id, value
) VALUES (
(0, 'EN'),
(1, 'DE'),
(4, 'ES')
); |
I apologize for the delayed response. @Martmists all data types should be supported since the Enum name is what gets stored in the db. @luckydonald it will store the Enum name Another thing to consider is that some dbs support enumerations which may be a better way to represent the data. This is not something that these changes support. @luckydonald your Language example is how it will look in the db. |
@cdraf I added a But, now that I think about it, enums are all about storing some arbitrary value behind an textual key. |
is there any chance this will be merged? I'm looking for an orm with simple enum support. |
@cdraf class Language(Enum):
EN = 0
DE = 1
ES = 4 becomming DROP TABLE IF EXISTS Language;
CRATE TABLE Language
id INTEGER PRIMARY KEY,
value TEXT NOT NULL
;
INSERT INTO Language (
id, value
) VALUES (
(0, 'EN'),
(1, 'DE'),
(4, 'ES')
); and class SomeTable(db.Entity):
id = Autoinclement()
lang = Required(Language) becomes CRATE TABLE SomeTable
id INTEGER PRIMARY KEY,
lang INTEGER NOT NULL REFERENCES Language.id
; So will it have a table for the enum, and reference that one? |
Hi, @kozlovsky! Is there any chance this will be merged or supported by Pony ORM directly soon? |
also looking forward to this |
I made a simpler approach (#502) which will make the database actually store the value, and not as proposed here, the key of the enum. This should allow the same usage from a python side, but makes the handling of the values in the database trivial, as an Also the table column definition would directly correspond to the definition in the table, making upgrading from a normal field to a Enum based field easy as well.
|
Can this be modified to use native database enum support where available (e.g. postgres)? |
Added an enum converter that creates a text column in the db.
Added Enum type support so query syntax (select/filter/where) can be used and a unittest (test_enum.py) that demonstrates each.
To support Enum in python 2.7 and 3.3 I've changed setup.py to conditionally add a dependency on pypi enum34.