Skip to content

Instantly share code, notes, and snippets.

@mohitt
Created January 23, 2022 01:42
Show Gist options
  • Save mohitt/da9cc416fe43baf0d7f8305b8c4b3447 to your computer and use it in GitHub Desktop.
Save mohitt/da9cc416fe43baf0d7f8305b8c4b3447 to your computer and use it in GitHub Desktop.
Exception in sql alchemy
# Session created using session maker, asyncpg connection string
engine = self.active_session.get_bind()
table_name = model_class.__tablename__
schema_name = model_class.__table_args__["schema"]
return await engine.dialect.has_table(
self.active_session, table_name, schema_name
)
`has_table` throws following error.
def has_table(self, connection, table_name, schema=None):
# seems like case gets folded in pg_class...
if schema is None:
cursor = connection.execute(
sql.text(
"select relname from pg_class c join pg_namespace n on "
"n.oid=c.relnamespace where "
"pg_catalog.pg_table_is_visible(c.oid) "
"and relname=:name"
).bindparams(
sql.bindparam(
"name",
util.text_type(table_name),
type_=sqltypes.Unicode,
)
)
)
else:
cursor = connection.execute(
sql.text(
"select relname from pg_class c join pg_namespace n on "
"n.oid=c.relnamespace where n.nspname=:schema and "
"relname=:name"
).bindparams(
sql.bindparam(
"name",
util.text_type(table_name),
type_=sqltypes.Unicode,
),
sql.bindparam(
"schema",
util.text_type(schema),
type_=sqltypes.Unicode,
),
)
)
> return bool(cursor.first())
E AttributeError: 'coroutine' object has no attribute 'first'
../../../.virtualenvs/vocore-IesooB9o/lib/python3.9/site-packages/sqlalchemy/dialects/postgresql/base.py:3336: AttributeError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment