question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'Engine' object has no attribute 'connection' when calling SQLAlchemy's `get_columns` with an `Engine`.

See original GitHub issue

Hey @laughingman7743,

First of all, thanks a ton for putting together this nice Python wrapper for AWS Athena and also including a SQLAlchemy interface!

Sadly, when trying it out with Superset, I ran into the following issue:

  File "/root/venv/lib/python3.5/site-packages/pyathena/sqlalchemy_athena.py", line 187, in get_columns
    stop=stop_after_attempt(connection.connection.retry_attempt),
AttributeError: 'Engine' object has no attribute 'connection'

The trouble is that PyAthena expects a Connection in its get_columns interface (https://github.com/laughingman7743/PyAthena/blob/master/pyathena/sqlalchemy_athena.py#L157). In particular, it expects that connection.connection will exist, which sadly is not the case when connection is of the Engine type.

I am not familiar enough with SQLAlchemy to comment on what should be done differently, but this greatly hampers the usability of PyAthena with Superset. My current fix is to use PyAthenaJDBC (thank you for putting that together as well!), which is significantly slower.

Thus, I would like to see what can be done to fix this in PyAthena, as it otherwise works very well.

I am also happy to put together a PR with the fix – I’d just like to know your thoughts on what approach would work best with the overall architecture.

To make testing simpler, I’ve also put together a minimal example which demonstrates this issue.

from urllib.parse import quote_plus  # PY2: from urllib import quote_plus
from sqlalchemy.engine import create_engine
import sqlalchemy

conn_str = 'awsathena+rest://{aws_access_key_id}:{aws_secret_access_key}@athena.{region_name}.amazonaws.com:443/'\
           '{schema_name}?s3_staging_dir={s3_staging_dir}'
engine = create_engine(conn_str.format(
    aws_access_key_id=quote_plus('AWS_ACCESS_KEY_ID'),
    aws_secret_access_key=quote_plus('AWS_SECRET_ACCESS_KEY'),
    region_name='eu-west-1',
    schema_name='default',
    s3_staging_dir=quote_plus('s3://some-bucket')))
inspector = sqlalchemy.inspect(engine)
print(inspector.get_columns('table_name', 'schema_name'))

Thanks a ton again!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mrshucommented, Feb 7, 2019

@laughingman7743 I believe one way of doing that would be by calling .raw_connection() on the passed first argument (connection) when its type is Engine.

Do you think that may work? You may then be able to access the underlying DB-API connection.

0reactions
mrshucommented, Feb 9, 2019

The first argument connection seems to be sqlalchemy.engine.Engine or sqlalchemy.engine.base.Connection. I try to check the instance type.

Makes a ton of sense – seems like that indeed fixed the issue for me.

Thanks a ton for your prompt responses – once you merge and release #65 I believe this issue can be closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQLAlchemy 1.4 tutorial code "'Connection' object has no ...
You are getting the "'Connection' object has no attribute 'commit'" error because you are creating an old-style Engine object.
Read more >
Working with Engines and Connections — SQLAlchemy 2.0 ...
This section details direct usage of the Engine , Connection , and related objects. Its important to note that when using the SQLAlchemy...
Read more >
Working with Engines and Connections — SQLAlchemy 1.3 ...
Above, we associate an Engine with a MetaData object using the special attribute MetaData.bind . The select() construct produced from the Table object...
Read more >
Working with Engines and Connections
This section describes how to use transactions when working directly with Engine and Connection objects. When using the SQLAlchemy ORM, the public API...
Read more >
Reflecting Database Objects - SQLAlchemy 1.4 Documentation
The above operation will use the given engine to query the database ... the table's attributes will be loaded if they have not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found