AttributeError: Neither 'QueryableAttribute' object nor 'Comparator' object has an attribute '__visit_name__'

1,444 views
Skip to first unread message

Dane K Barney

unread,
Mar 23, 2021, 9:35:31 PM3/23/21
to sqlalchemy
I am using SQLAlchemy 1.4.2 and have been encountering this strange error message which I've managed to boil down to this minimal example:

import sqlalchemy as sa
from sqlalchemy.orm import declarative_base, relationship

Base = declarative_base()

class Parent(Base):
    __tablename__ = "parent"
    id = sa.Column(sa.Integer, primary_key=True)


class Child(Base):
    __tablename__ = "child"
    id = sa.Column(sa.Integer, primary_key=True)
    parent_id = sa.Column(sa.Integer, sa.ForeignKey("parent.id"))
    parent = relationship("Parent")


statement = sa.select([Child.id]).join(Child.parent).where(Parent.id == sa.bindparam("id"))

statement = statement.params(id=10)


The call to statement.params(...) is throwing the following stacktrace. Can anyone tell me what I'm doing wrong?

Traceback (most recent call last):
  File "test_sqlalchemy_error.py", line 20, in <module>
    statement = statement.params(id=10)
  File ".venv/lib/python3.7/site-packages/sqlalchemy/sql/elements.py", line 346, in params
    return self._replace_params(False, optionaldict, kwargs)
  File ".venv/lib/python3.7/site-packages/sqlalchemy/sql/elements.py", line 363, in _replace_params
    return cloned_traverse(self, {}, {"bindparam": visit_bindparam})
  File ".venv/lib/python3.7/site-packages/sqlalchemy/sql/visitors.py", line 742, in cloned_traverse
    obj = clone(obj, deferred_copy_internals=deferred_copy_internals)
  File ".venv/lib/python3.7/site-packages/sqlalchemy/sql/visitors.py", line 735, in clone
    newelem._copy_internals(clone=clone, **kw)
  File ".venv/lib/python3.7/site-packages/sqlalchemy/sql/selectable.py", line 5208, in _copy_internals
    clone=clone, omit_attrs=("_from_obj",), **kw
  File ".venv/lib/python3.7/site-packages/sqlalchemy/sql/traversals.py", line 718, in _copy_internals
    result = meth(attrname, self, obj, **kw)
  File ".venv/lib/python3.7/site-packages/sqlalchemy/sql/traversals.py", line 777, in visit_setup_join_tuple
    for (target, onclause, from_, flags) in element
  File ".venv/lib/python3.7/site-packages/sqlalchemy/sql/traversals.py", line 777, in <genexpr>
    for (target, onclause, from_, flags) in element
  File ".venv/lib/python3.7/site-packages/sqlalchemy/sql/visitors.py", line 736, in clone
    meth = visitors.get(newelem.__visit_name__, None)
  File ".venv/lib/python3.7/site-packages/sqlalchemy/orm/attributes.py", line 308, in __getattr__
    replace_context=err,
  File ".venv/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 198, in raise_
    raise exception
AttributeError: Neither 'QueryableAttribute' object nor 'Comparator' object associated with Child.parent has an attribute '__visit_name__'


Mike Bayer

unread,
Mar 24, 2021, 8:41:52 AM3/24/21
to noreply-spamdigest via sqlalchemy
hi -

that's a bug!    The .params() method is kind of forgotten.     https://github.com/sqlalchemy/sqlalchemy/issues/6124 is added, however this is not a regression since this is using new functionality on select().   I think I have a fairly straightforward fix for this however it won't be in SQLAlchemy 1.4.3, which needs to go out very soon.

For now I would advise sending the params directly to the session.execute() method.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages