Gino: How to create an Enum type in GINO

Created on 15 Mar 2018  Â·  9Comments  Â·  Source: python-gino/gino

  • GINO version: 0.6
  • Python version: 3.6.4
  • Operating System: Debian Testing

Description

Trying to define an Enum like so (test_enum.py):

from enum import Enum

import pytest

import gino

from .models import PG_URL


pytestmark = pytest.mark.asyncio
db = gino.Gino()


async def test_enum():
    class MyEnum(Enum):
        ONE = 'one'
        TWO = 'two'

    class Model(db.Model):
        __tablename__ = 'model'

        number = db.Column(db.Enum(MyEnum), nullable=False, default=MyEnum.TWO)

    await db.set_bind(PG_URL)
    await db.gino.create_all()

What I Did

Shouldn't create_all also create the enum type?

/sqlalchemy/dialects/postgresql/base.py:1284: RuntimeWarning: coroutine 'AsyncpgDialect.has_type' was never awaited

tests/test_enum.py:25:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
gino/schema.py:301: in create_all
    await self.create(bind=bind, tables=tables, checkfirst=checkfirst)
gino/schema.py:291: in create
    self._item, *args, **kwargs)
gino/engine.py:755: in _run_visitor
    await getattr(conn, '_run_visitor')(*args, **kwargs)
gino/engine.py:482: in _run_visitor
    **kwargs).traverse_single(element)
gino/schema.py:12: in traverse_single
    return await meth(obj, **kw)
gino/schema.py:71: in visit_metadata
    _is_metadata_operation=True)
gino/schema.py:12: in traverse_single
    return await meth(obj, **kw)
gino/schema.py:105: in visit_table
    include_foreign_key_constraints=include_foreign_key_constraints
gino/engine.py:372: in status
    return await result.execute(status=True)
gino/dialects/base.py:143: in execute
    context.statement, context.timeout, args, 1 if one else 0)
gino/dialects/asyncpg.py:129: in async_execute
    query, executor, timeout)
venv/py36/lib/python3.6/site-packages/asyncpg/connection.py:1333: in _do_execute
    result = await executor(stmt, None)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

>   ???
E   asyncpg.exceptions.UndefinedObjectError: type "myenum" does not exist

asyncpg/protocol/protocol.pyx:203: UndefinedObjectError
========================================================= warnings summary =========================================================
tests/test_enum.py::test_enum
  /home/olaf/src/gino/venv/py36/lib/python3.6/site-packages/sqlalchemy/dialects/postgresql/base.py:1284: RuntimeWarning: coroutine 'AsyncpgDialect.has_type' was never awaited
    bind, self.name, schema=self.schema):

-- Docs: http://doc.pytest.org/en/latest/warnings.html
========================================= 1 failed, 64 passed, 1 warnings in 23.71 seconds =========================================
bug

Most helpful comment

Good catch! This issue exist in the unported SQLAlchemy event system and sqlalchemy.sql.sqltypes.SchemaType. Fixing this one would be quite tricky, and might take some time. Before the fix, I'd suggest using normal non-async SQLAlchemy to create_all, or simply use Alembic.

All 9 comments

Good catch! This issue exist in the unported SQLAlchemy event system and sqlalchemy.sql.sqltypes.SchemaType. Fixing this one would be quite tricky, and might take some time. Before the fix, I'd suggest using normal non-async SQLAlchemy to create_all, or simply use Alembic.

How come the Enum used in
https://github.com/fantix/gino/blob/master/tests/models.py#L21
does work?
How is that one created?

@oohlaf it was created by SQLAlchemy non-async engine: https://github.com/fantix/gino/blob/master/tests/conftest.py#L17

ah, ok, and probably uses psycopg2 driver
thanks

Yep, np.

Didn't think it was not very tough. 😸

Cool, I'll have a look when I get home tonight.

Thanks works like a charm ✨

Good to know! I’ll release 0.6.1 for this

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sapph1re picture sapph1re  Â·  5Comments

saeedghx68 picture saeedghx68  Â·  4Comments

KoustavCode picture KoustavCode  Â·  3Comments

myraygunbarrel picture myraygunbarrel  Â·  3Comments

ape364 picture ape364  Â·  6Comments