I noticed an issue that - well, I honestly don't know whether to call
it a "bug" or not.  I need to talk it out with somebody who
understands SQLA better.

When I retrieve an instance, then alter its attributes by manipulating
its __dict__, the instance doesn't get "dirtied", and the changes
won't be sent to the database when I flush the session.

My reason: I've got a program that retrieves rows from a database,
then applies a bunch of operations (stored as text) to each row, sort
of like so:

operations = [
'if firstname in ('Bob', 'Sam') then performance_review = "lazy"',
'if department == "IT" then vacation_days += 10']

for operation in operations:
    for employee in employees.select():
        exec(operation, employee.__dict__)

By passing the __dict__ to exec, each operation works directly on the
fields of each instance.   But afterward, my session's .dirty remains
empty, and the changes don't get flushed.

I can work around the problem pretty easily, like this:
        exec(operation, employee.__dict__)
        employee._state['modified'] = True

That change to ._state normally happens in
InstrumentedAttribute.__set__ in sqlalchemy/orm/attributes.py (line
263), which gets called for "instance.attribute = newvalue", but not
for "instance.__dict__['attribute'] = newvalue".

I'm wondering if I should just sit on this workaround personally, or
dig into figuring out how to modify SQLA so it will always work for
__dict__ manipulation.

So - is direct manipulation of the __dict__ something that SQLA should
support, or would that fall in the realm of "you are getting weird on
us, you're on your own"?  I guess that double-underscore sort of
implies "you're on your own", but __dict__ manipulation doesn't seem
overly exotic in practice.

Thanks,
- Catherine
http://catherinedevlin.blogspot.com


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to