Reputation: 61
I have recently updated my SQLAlchemy and FormAlchemy to the newest ones and a strange thing has occurred.
I do use Mako Templates to display data from my model.
Python:
class Asset(object):
id = Column(Integer, primary_key=True, nullable=False)
...
@hybrid_property
def underscore_name(self):
return "x_underscore_name_x"
Mako template:
<li>Item: ${Asset.underscore_name}</li>
Before the upgrade the web page rendered text was:
Item: x_underscore_name_x
After the upgrade it shows:
Item: Asset.underscore_name
Important! The method is being executed but the returned result is not being rendered on the webpage. Any ideas?
EDIT:
The library responsible for this behaviour is SQL Alchemy >=1.1.0. Version 1.0.19 does not have this issue. Let's see what's the response from the developers.
Upvotes: 0
Views: 264
Reputation: 61
By the courtesy of Michael Bayer who commented on reported issue #4214: hybrid_property name being displayed rather than the returned value
The value of Asset.underscore_name is a SQL expression, which since the changes described in http://docs.sqlalchemy.org/en/latest/changelog/migration_11.html#hybrid-properties-and-methods-now-propagate-the-docstring-as-well-as-info is derived from the clause_element() method of the returned object that Core understands. The hybrid you illustrate above is not correctly written because it is not returning a Core SQL expression object:
s = Session()
print(s.query(Asset.underscore_name))
sqlalchemy.exc.InvalidRequestError: SQL expression, column, or mapped entity expected - got ''x_underscore_name_x''
if you are looking for a class-bound attribute to return a string, just assign it:
class Asset(...):
underscore_name = "x_underscore_name_x"
or if you want that to be a method that is callable at the classlevel, use a @classproperty, there's a recipe for that here: https://stackoverflow.com/a/5191224/34549 and SQLAlchemy has one you can copy: https://github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalchemy/util/langhelpers.py#L1140
IMO there's no bug here because this is not the correct usage of @hybrid_property
Thank you very much Michael!
Upvotes: 0