Skip to content

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

Open
@amotl

Description

@amotl

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions