I'm using the separate table versioning code from *history_meta.py*. When I 
try to save a new object, I get this:

  [...]
  File "/usr/local/lib/python3.4/dist-packages/sqlalchemy/orm/session.py", 
line 1282, in _autoflush
  self.flush()
  File "/usr/local/lib/python3.4/dist-packages/sqlalchemy/orm/session.py", 
line 2004, in flush
    self._flush(objects)
  File "/usr/local/lib/python3.4/dist-packages/sqlalchemy/orm/session.py", 
line 2031, in _flush
    self.dispatch.before_flush(self, flush_context, objects)
  File "/usr/local/lib/python3.4/dist-packages/sqlalchemy/event/attr.py", 
line 218, in __call__
    fn(*args, **kw)
  File "/usr/share/foo/app/server/history_meta.py", line 290, in 
before_flush
    create_version(obj, session)
  File "/usr/share/foo/app/server/history_meta.py", line 256, in 
create_version
    attr[hist_col.key] = a[0]
IndexError: tuple index out of range

I added some logging, and it seems that this happens for attributes that I 
haven't assigned a value to, and which have a default value of *None*. - 
because in this line in *create_version*, *a*, *u* and *d* are all empty:

a, u, d = attributes.get_history(obj, prop.key)

If I actually assign *None* to the attribute before saving, then it works 
fine. It also works if I apply this patch to *history_meta.py*:

diff --git a/src/app/server/history_meta.py b/src/app/server/history_meta.py
index 02b54e0..17cd87f 100644
--- a/src/app/server/history_meta.py
+++ b/src/app/server/history_meta.py
@@ -240,10 +240,13 @@ def create_version(obj, session, deleted=False):
                 obj_changed = True
             elif u:
                 attr[hist_col.key] = u[0]
-            else:
+            elif a:
                 # if the attribute had no value.
                 attr[hist_col.key] = a[0]
                 obj_changed = True
+            else:
+                # The attribute has never had a value
+                attr[hist_col.key] = None
 
     if not obj_changed:
         # not changed, but we have relationships.  OK

Is this a reasonable thing to do?

Cheers,
Alex

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to