Hybrid property "can't set attribute"

341 views
Skip to first unread message

YKdvd

unread,
Dec 19, 2019, 3:34:34 PM12/19/19
to sqlalchemy
We have a "Users" model with this, which was a hybrid property to wrap the "email" column temporarily.  The database column (MySQL 5.7)  is "email", but defined by ORM as "_email", with an "email" hybrid property to access and set:

_email = Column(u'email', String(255))
...
@hybrid_property
def email(self):
 
return self._email.replace("olddomain.com", "newdomain.com")
@email.setter
def email_setter(self, val):
 
self._email = val

In 1.1.18, something like "self.email = someEmailAddress" works fine.  We're testing an upgrade to 1.3.11, and that now throws an "AttributeError: can't set attribute" from hybrid.py __set__(). 
That seems to be at a simple check "if self.fset is None", so it's almost as if the decorator never stored the setter function?  I'm digging into the hybrid docs, and it seems a pretty innocuous setter, but there might be something about it that
1.3 is being stricter about?  I don't see any SAwarnings at startup that might apply.  I changed the getter to simply return self._email in case that was a problem, but that didn't help.

Mike Bayer

unread,
Dec 19, 2019, 7:18:48 PM12/19/19
to noreply-spamdigest via sqlalchemy
hiya -

going to say right off this is the naming issue, there's an unfortunately stalled PR to make sure the documentation emphasizes this at https://github.com/sqlalchemy/sqlalchemy/pull/4970 .   basically the same note as you see in https://docs.sqlalchemy.org/en/13/orm/extensions/hybrid.html#defining-expression-behavior-distinct-from-attribute-behavior.    basically you are assigning your setter function to a brand new hybrid called "email_setter".  name it "def email(self, val)" instead.
--
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.

Mike Bayer

unread,
Dec 19, 2019, 7:19:48 PM12/19/19
to noreply-spamdigest via sqlalchemy

YKdvd

unread,
Dec 19, 2019, 7:43:02 PM12/19/19
to sqlalchemy
Ah, thanks very much, that's fixed it.  I must have missed that in the docs - I think this started out as a Python property and they later added the hybrid decorator.
Reply all
Reply to author
Forward
0 new messages