Reputation: 92440
I thought I was following the docs pretty closely setting up an ENUM field in a Postgres DB with sqlalchemy, but I'm clearly doing something (hopefully something simple) wrong.
My table has a type contact_type
:
List of data types
Schema | Name | Internal name | Size | Elements | Owner | Access privileges | Description
--------+---------------+---------------+------+---------------+----------+-------------------+-------------
public | contact_types | contact_types | 4 | unknown +| postgres | |
| | | | incoming_text+| | |
| | | | incoming_call+| | |
| | | | outgoing_call | | |
and in the table:
Table "public.calls"
Column | Type | Modifiers
--------------+--------------------------+----------------------------------------------------
contact_type | contact_types |
In python I created a subclass of enum
per the docs:
import enum
class contact_types(enum.Enum):
unknown: 1
incoming_text: 2
incoming_call: 3
outgoing_call: 4
and passed it to the model:
class Call(db.Model):
contact_type = db.Column(db.Enum(contact_types))
It all looked good. Inserts work and I can see the values when looking at the table, but SQLAlchemy's validation seems to be unhappy when querying. This leads to an error:
calls = Call.query.order_by(Call.time.desc()).limit(pagesize).offset(offset)
for c in calls:
print(c)
LookupError: "unknown" is not among the defined enum values
'unknown' is in the Enum. Am I missing a step somewhere to connect the query to the enum class?
Upvotes: 3
Views: 2309
Reputation: 15090
there should be =
in enum
definition, not :
class contact_types(enum.Enum):
unknown = 1
incoming_text = 2
incoming_call = 3
outgoing_call = 4
Upvotes: 4