Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: Neither 'BinaryExpression' object nor 'Comparator' object has an attribute 'astext' #188

Open
amotl opened this issue Dec 14, 2024 · 0 comments

Comments

@amotl
Copy link
Member

amotl commented Dec 14, 2024

Problem

When deriving a CrateDB adapter for LangChain from langchain-postgres, we are observing this error when invoking a test case.

pytest -vvv -k test_cratedb_with_filter_in_set
self = <sqlalchemy.sql.elements.BinaryExpression object at 0x105f2b860>, 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:

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:

from sqlalchemy import cast

cast(
    data_table.c.data['some_key'], String
) == '"some_value"'

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant