SQL expression object expected, got object of type <class 'property'> instead

1,518 views
Skip to first unread message

Simon

unread,
Jan 23, 2022, 9:03:05 PM1/23/22
to sqlalchemy
Hi there,

I got a problem about 'sqlalchemy.exc.ArgumentError: SQL expression object expected, got object of type <class 'property'> instead'

My SQLAlchemy version is 1.3.22. I have a database like 

Class Genome:
    id = Column(Integer, primary_key=True)
    created_date = Column(Datetime, nullable=False)

@property
def attributes(self):
     return "something"


If using the query, it reports an error through the elements.py in the sqlAlchemy

def _literal_as(element, text_fallback):
if isinstance(element, Visitable):
return element
elif hasattr(element, "__clause_element__"):
return element.__clause_element__()
elif isinstance(element, util.string_types):
return text_fallback(element)
elif isinstance(element, (util.NoneType, bool)):
return _const_expr(element)
else:
raise exc.ArgumentError(
"SQL expression object expected, got object of type %r "
"instead" % type(element)
)

This exception is not raised if I directly query genome's column name such as created_date or id. 

I am wondering 1) could the column name and property be used interchangeably in some way? Or say how could to query a table's property in the way of querying a table's column? 2) what are some significant differences between table's column name and property?

Thanks.

 


Simon King

unread,
Jan 25, 2022, 6:53:52 AM1/25/22
to sqlal...@googlegroups.com
Can you show the part of *your* code that is triggering the error, and
explain what you are trying to do? Plain python properties aren't
normally very useful when accessed via a class. "Genome.attributes"
returns a property object, not the return value from the function, and
I don't understand what you are trying to do with it.

Thanks,

Simon
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> 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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/7f03c9bb-9324-4e3e-8aea-cd0d46f9021bn%40googlegroups.com.

Simon

unread,
Jan 26, 2022, 6:30:29 PM1/26/22
to sqlalchemy
Sorry for that the question is not clear. The question is how can we query a database's property.

Given the above example, if query(Genome.id) or query(Genome.created_date), it works fine. But if I query the property, query(Genome.attributes): it raises the exception. 

Thanks.
Simon

Simon King

unread,
Jan 27, 2022, 10:16:05 AM1/27/22
to sqlal...@googlegroups.com
I'm sorry, I still don't understand - what do you expect
query(Genome.attributes) to do? Can you give an example?

Thanks,

Simon
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/2fbf2d83-6865-4b9b-8db6-cb5f60e0eaffn%40googlegroups.com.

Simon

unread,
Feb 1, 2022, 10:14:15 PM2/1/22
to sqlalchemy
Never mind. This problem is solved by join operations.

 Thanks for your attention.
Reply all
Reply to author
Forward
0 new messages