Enum in Array

715 views
Skip to first unread message

Tim-Christian Mundt

unread,
Jan 5, 2017, 1:34:47 AM1/5/17
to sqlal...@googlegroups.com
Hi,

I've been using an array of enums with postgres and SQLAlchemy successfully over the past year like so:

class MyModel(BaseModel):
    enum_field = Column(postgresql.ARRAY(EnumField(MyEnum, native_enum=False)))

The EnumField is from the sqlalchemy_enum34 library, a small wrapper around the builtin enum that uses Python enums as Python representation instead of strings.

Although the docs say, array of enum is not supported, I guess it worked, because I chose 'native_enum=False'. Recently I noticed that it doesn't work anymore, I think it's due to the upgrade from SQLA 1.0 to 1.1, but I'm not sure.

The problem is, that it generates invalid DDL:

CREATE TABLE my_model (
    enum_field VARCHAR(5)[3] NOT NULL CHECK (contexts IN ('ONE', 'TWO', 'THREE'))
)

The error I get is:

ERROR:  malformed array literal: "ONE"
DETAIL:  Array value must start with "{" or dimension information.

Any idea how I can get back my enum array?
By the way: when it worked, no CHECK constraint was actually created, just an array of varying. I'm ok with that as long as I can use enums in my Python code (e.g. query.filter(enum_field==MyEnum.ONE))

Regards
Tim

mike bayer

unread,
Jan 5, 2017, 8:53:20 AM1/5/17
to sqlal...@googlegroups.com
you probably want to add create_constraint=False, see if that works

http://docs.sqlalchemy.org/en/latest/core/type_basics.html?highlight=enum#sqlalchemy.types.Enum.params.create_constraint



On 01/05/2017 01:34 AM, Tim-Christian Mundt wrote:
> Hi,
>
> I've been using an array of enums with postgres and SQLAlchemy
> successfully over the past year like so:
>
> |classMyModel(BaseModel):enum_field
> =Column(postgresql.ARRAY(EnumField(MyEnum,native_enum=False)))|
>
> The |EnumField| is from the sqlalchemy_enum34
> <https://github.com/spoqa/sqlalchemy-enum34> library, a small wrapper
> around the builtin enum that uses Python enums as Python representation
> instead of strings.
>
> Although the docs say
> <http://docs.sqlalchemy.org/en/rel_1_1/dialects/postgresql.html#using-enum-with-array>,
> array of enum is not supported, I guess it worked, because I chose
> 'native_enum=False'. Recently I noticed that it doesn't work anymore, I
> think it's due to the upgrade from SQLA 1.0 to 1.1, but I'm not sure.
>
> The problem is, that it generates invalid DDL:
>
> |CREATE TABLE my_model (enum_field VARCHAR(5)[3]NOT NULL CHECK (contexts
> IN ('ONE','TWO','THREE')))|
>
> The error I get is:
>
> |ERROR: malformed array literal:"ONE"DETAIL: Arrayvalue must start
> with"{"ordimension information.|
>
> Any idea how I can get back my enum array?
> By the way: when it worked, no CHECK constraint was actually created,
> just an array of varying. I'm ok with that as long as I can use enums in
> my Python code (e.g. |query.filter(enum_field==MyEnum.ONE)|)
>
> Regards
> Tim
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google
> Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to sqlalchemy+...@googlegroups.com
> <mailto:sqlalchemy+...@googlegroups.com>.
> To post to this group, send email to sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

Tim-Christian Mundt

unread,
Jan 5, 2017, 9:01:48 AM1/5/17
to sqlal...@googlegroups.com
That works, thanks.

There is no (easy) way to CHECK the elements of the array?
> To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
> To post to this group, send email to sqlal...@googlegroups.com.

mike bayer

unread,
Jan 5, 2017, 9:47:43 AM1/5/17
to sqlal...@googlegroups.com
the enum type should be doing that check client side at this point,
which is usually good enough. I would not have done the CHECK
constraint feature of enum/boolean by default if it were today it has
caused enormous problems.

Tim-Christian Mundt

unread,
Jan 5, 2017, 9:49:59 AM1/5/17
to sqlal...@googlegroups.com
Ok, since I’m using an enum it’s practically impossible to insert invalid data anyways. Thanks a lot.
Reply all
Reply to author
Forward
0 new messages