Automatic check for invalid property names on mapped objects

0 views
Skip to first unread message

Barry

unread,
Apr 23, 2007, 12:46:46 PM4/23/07
to SQLElixir
I am porting a medium-sized (~25,000 lines of code) application from
SQLObject to SQLAlchemy. A very common bug we encountered was with the
different property names in SQLAlchemy vs. SQLObject. If I forgot to
update some code that sets a property, a new property would be created
on the object but then discarded when the object went away. These bugs
often took a long time to discover (although once discovered, they
were easy to fix).

To make my life easier, I developed a custom __setattr__ function
which throws an error on any attempt to set an unknown property name.
Would it be useful to include this behavior as an option in SQLElixir?
If so, I can make the code available.

Barry

Gaetan de Menten

unread,
Apr 23, 2007, 12:58:55 PM4/23/07
to sqle...@googlegroups.com
I'm not sure on this one. What I'm sure of is that we can't have this
on by default. The problem is that, from what I understand, such a
code will trigger errors for simply having instance attributes which
are not persisted. I'd either include this as an option, or make an
example/FAQ out of it. My personal vote is for the second option.

Btw: the code you speak of is the one at:
http://www.sqlalchemy.org/trac/attachment/ticket/547/code.txt
right?


--
Gaëtan de Menten
http://openhex.org

Barry

unread,
Apr 23, 2007, 2:17:57 PM4/23/07
to SQLElixir
>on by default. The problem is that, from what I understand, such a
>code will trigger errors for simply having instance attributes which
>are not persisted. I'd either include this as an option, or make an

That's right. Another concern is performance, due to the overhead of
additional checking. For that reason, most people would only want to
do this in development code, not production.

There is a pretty easy workaround for transient attributes - you can
use Python's property feature, where you define setter and getter
methods for the property -- as written, the error checking function
detects and allows these to be set.

I see this function mainly as an aid in porting from SQLObject; once
someone is writing new code on SQLAlchemy, they will most likely catch
attribute naming errors in their normal code/debug cycle.

>http://www.sqlalchemy.org/trac/attachment/ticket/547/code.txt
>right?

Yes.

Barry

On Apr 23, 12:58 pm, "Gaetan de Menten" <gdemen...@gmail.com> wrote:
> I'm not sure on this one. What I'm sure of is that we can't have this
> on by default. The problem is that, from what I understand, such a
> code will trigger errors for simply having instance attributes which
> are not persisted. I'd either include this as an option, or make an
> example/FAQ out of it. My personal vote is for the second option.
>
> Btw: the code you speak of is the one at:http://www.sqlalchemy.org/trac/attachment/ticket/547/code.txt
> right?
>

Reply all
Reply to author
Forward
0 new messages