crate / crate/sqlalchemy-cratedb
`AttributeError: Neither 'BinaryExpression' object nor 'Comparator' object has an attribute 'astext'`
- Dominant language
- Python
- Stars
- 9
- Forks
- 4
- Avg merge
- 6d 12h
- Merged PRs (30d)
- 3
Description
## Problem
When deriving a CrateDB adapter for LangChain from [langchain-postgres](https://github.com/langchain-ai/langchain-postgres), we are observing this error when invoking a test case.
```
pytest -vvv -k test_cratedb_with_filter_in_set
```
```python
self = , key = 'astext'
def __getattr__(self, key: str) -> Any:
try:
return getattr(self.comparator, key)
except AttributeError as err:
> raise AttributeError(
"Neither %r object nor %r object has an attribute %r"
% (
type(self).__name__,
type(self.comparator).__name__,
key,
)
) from err
E AttributeError: Neither 'BinaryExpression' object nor 'Comparator' object has an attribute 'astext'
.venv/lib/python3.12/site-packages/sqlalchemy/sql/elements.py:1498: AttributeError
```
## Solution
"astext" is part of the psycopg2 variant of JSON, use it like this:
```python
from sqlalchemy dialects import postgresql
class Student(db.Model):
# ...
data_test=db.Column(postgresql.JSON)
```
if you're using plain JSON, you should use cast:
```python
from sqlalchemy import cast
cast(
data_table.c.data['some_key'], String
) == '"some_value"'
```
## References
- https://www.mail-archive.com/sqlalchemy@googlegroups.com/msg40744.html
- https://github.com/sqlalchemy/alembic/issues/1386
- https://stackoverflow.com/questions/49742659/sqlalchemy-cannot-query-json-comparator-object-has-an-attribute-as-text
Contributor guide
Research direction
Start by running `pytest -vvv -k test_cratedb_with_filter_in_set` and trace the JSON expression used by the CrateDB dialect. Compare the failing `.astext` access with SQLAlchemy's JSON and `cast` behavior; done means the test passes without the AttributeError and JSON filtering remains correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, sqlalchemy
- Domain
- database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100