On Mon, Jan 7, 2019 at 7:06 PM Rich Shepard <[email protected]> wrote:
>
> On Mon, 7 Jan 201U9, Mike Bayer wrote:
>
> > Postgresql has ENUM within CREATE TYPE:
> > https://www.postgresql.org/docs/9.1/datatype-enum.html
>
> Mike,
>
> Thank you. I hadn't looked at the postgres docs for it.
>
> > I'm not sure if this is a "DOMAIN" behind the scenes or what.
>
> Postgres supports the SQL DOMAIN. Whether enum is equivalent is yet to be
> determined. :-)
>
> > This SQL syntax is directly supported with the PG ENUM type:
> > https://docs.sqlalchemy.org/en/latest/dialects/postgresql.html?highlight=enum#enum-types
> > which is also available from the generic enum type:
> > https://docs.sqlalchemy.org/en/latest/core/type_basics.html#sqlalchemy.types.Enum,
> > as long as native_enum=True on that object.
> >
> > Enum is pretty popular so it should work well.
>
> Thanks for the URLs. I glanced at the SA constraint doc's section on enum
> but have not yet carefully read it.
>
> I'm using the domain because the same column check constraint applies to
> two tables in this application so it makes sense to write it once and have
> it applied everywhere there's a state_code column.
PG's ENUM does work that way, below the same type is shared:
from sqlalchemy import Column
from sqlalchemy import create_engine
from sqlalchemy import Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session
from sqlalchemy.dialects import postgresql
Base = declarative_base()
State = postgresql.ENUM("AK", "AL", "CO", "NY", name="states")
class A(Base):
__tablename__ = 'a'
id = Column(Integer, primary_key=True)
data = Column(State)
class B(Base):
__tablename__ = 'b'
id = Column(Integer, primary_key=True)
data = Column(State)
e = create_engine("postgresql://scott:tiger@localhost/test", echo=True)
Base.metadata.create_all(e)
s = Session(e)
s.add_all([A(data="CO"), B(data="NY")])
s.commit()
SQL output includes:
CREATE TYPE states AS ENUM ('AK', 'AL', 'CO', 'NY')
CREATE TABLE a (
id SERIAL NOT NULL,
data states,
PRIMARY KEY (id)
)
CREATE TABLE b (
id SERIAL NOT NULL,
data states,
PRIMARY KEY (id)
)
>
> Best regards,
>
> Rich
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.