On 8/23/15 8:17 AM, Alex Fraser wrote:
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,inflush
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,inbefore_flush
    create_version(obj,session)
File"/usr/share/foo/app/server/history_meta.py",line 256,increate_version
    attr[hist_col.key]=a[0]
IndexError:tuple index outof 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..17cd87f100644
---a/src/app/server/history_meta.py
+++b/src/app/server/history_meta.py
@@-240,10+240,13@@defcreate_version(obj,session,deleted=False):
                 obj_changed =True
elifu:
                 attr[hist_col.key]=u[0]
-else:
+elifa:
# 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

ifnotobj_changed:
# not changed, but we have relationships.  OK
|

Is this a reasonable thing to do?
yes and no. Yes, if there's no "added" history, that should be skipped as you're doing, but no in that that particular line of code is not called if an object is being saved for the first time, only on an update, and then only if that attribute actually had a change, which you are saying this attribute did not.

so if you can please share: 1. a mapping and an exact usage that illustrates how this is happening 2. what exact version of SQLAlchemy are you using and 3. have you modified the history_meta.py recipe in any way?






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

--
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