MCPcopy Index your code
hub / github.com/sqlalchemy/sqlalchemy / ENUM

Class ENUM

lib/sqlalchemy/dialects/postgresql/named_types.py:192–390  ·  view source on GitHub ↗

PostgreSQL ENUM type. This is a subclass of :class:`_types.Enum` which includes support for PG's ``CREATE TYPE`` and ``DROP TYPE``. When the builtin type :class:`_types.Enum` is used and the :paramref:`.Enum.native_enum` flag is left at its default of True, the PostgreSQL backe

Source from the content-addressed store, hash-verified

190
191
192class ENUM(NamedType, type_api.NativeForEmulated, sqltypes.Enum):
193 """PostgreSQL ENUM type.
194
195 This is a subclass of :class:`_types.Enum` which includes
196 support for PG's ``CREATE TYPE`` and ``DROP TYPE``.
197
198 When the builtin type :class:`_types.Enum` is used and the
199 :paramref:`.Enum.native_enum` flag is left at its default of
200 True, the PostgreSQL backend will use a :class:`_postgresql.ENUM`
201 type as the implementation, so the special create/drop rules
202 will be used.
203
204 The create/drop behavior of ENUM tries to follow the PostgreSQL behavior,
205 with an usability improvement indicated below.
206
207 When using :class:`_types.Enum` or :class:`_postgresql.ENUM`
208 in an "inline" fashion, the ``CREATE TYPE`` is emitted
209 corresponding to when the :meth:`_schema.Table.create` method is called::
210
211 table = Table(
212 "sometable",
213 metadata,
214 Column("some_enum", ENUM("a", "b", "c", name="myenum")),
215 )
216
217 # will check if enum exists and emit CREATE ENUM then CREATE TABLE
218 table.create(engine)
219 table.drop(engine) # will *not* drop the enum.
220
221 The enum will not be dropped when the table is dropped, since it's
222 associated with the metadata, not the table itself. Call drop on the
223 :class:`_postgresql.ENUM` directly to drop the type::
224
225 metadata.get_schema_object_by_name("enum", "myenum").drop(engine)
226
227 To use a common enumerated type between multiple tables, the best
228 practice is to declare the :class:`_types.Enum` or
229 :class:`_postgresql.ENUM` independently::
230
231 my_enum = ENUM("a", "b", "c", name="myenum", metadata=metadata)
232
233 t1 = Table("sometable_one", metadata, Column("some_enum", myenum))
234
235 t2 = Table("sometable_two", metadata, Column("some_enum", myenum))
236
237 Like before, the type will be created if it does not exist::
238
239 # will check if enum exists and emit CREATE ENUM then CREATE TABLE
240 t1.create(engine)
241
242 The type will always be created and dropped if either the metadata-wide
243 create/drop is called::
244
245 metadata.create_all(engine) # will emit CREATE TYPE
246 metadata.drop_all(engine) # will emit DROP TYPE
247
248 The type can also be created and dropped directly::
249

Calls

no outgoing calls