Found your post with Google seeking the same.
Here is the solution, you handle this with a decorator:
def validate_column_attributes(original):
def __setattr__(self, key, value):
if key not in self.__table__.columns:
raise AttributeError("'%s' has no attribute '%s'" %
(self.__table__.name, key))
return original(self, key, value)
return __setattr__
Base = declarative_base()
Base.__setattr__ = validate_column_attributes(Base.__setattr__)
On Tuesday, November 13, 2012 6:05:24 PM UTC-7, MikeParent wrote:
>
> With a mapped class instance, if I try and set an unmapped attribute,
> Sqlalchemy simply adds this new attribute (locally) to the object, and
> subsequent 'get' operations return the assigned value.
>
> Base = declarative_base()
>
>
> class MyClass(Base):
>
> __tablename__ = 'some_table'
>
> id = Column(Integer, primary_key=True)
>
>
> ...
>
> obj = MyClass()
> ## This correctly raises 'AttributeError'
> print obj.unmapped_attribute
>
> ## This does not fail!
> obj.unmapped_attribute = 0
> ## Also does not fail anymore, prints "0"
> print obj.unmapped_attribute
>
> I'd like to have an 'AttributeError thrown whenever I try and set a bad
> property, similar to the getattr() behavior. Is this possible? Maybe I'm
> doing something wrong?
>
> (Using SqlAlchemy 0.7.9, Python 2.7)
>
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/sqlalchemy/-/BdK5IDajW8oJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.