Hello there,

my ORM classes use a mixin that defines a field 'editor_id' which 
references a user record.

When I load a freshly created record, editor_id is None, which is correct. 
Then I change some values, set editor_id, save it and load it again, 
editor_id is correctly set.

Now the strangeness happens: again I change some values and set editor_id, 
but use the same value for editor_id it already has, SA stores a NULL value.

This leads to funny alternating behaviour:
1st update: editor_id is set
2nd update: NULL
3rd update set
etc.

Am I doing sth wrong?

Thanks for your help.



Code to reproduce the behaviour (executed in a Pyramid web app):

    lgg = logging.getLogger('pym.test')
    sess = pym.models.DbSession()
    T = pym.authmgr.models.Tenant
    try:
        tn = sess.query(T).filter(T.name == 'foo').one()
    except sa.orm.exc.NoResultFound:
        tn = T()
        tn.owner_id = request.user.uid
        tn.name = 'foo'
        tn.descr = 'o'
        sess.add(tn)
        lgg.debug('Added')
        request.session['cnt'] = 0
    else:
        request.session['cnt'] += 1
        lgg.debug('{} {} {} {}'.format(request.session['cnt'], tn.editor_id,
            tn.descr, tn.descr.count('o')))
        tn.descr += 'o'
        tn.editor_id = request.user.uid

Its output:

2014-03-16 14:00:32,812 DEBUG [pym.test][Dummy-2] Added
2014-03-16 14:01:10,268 DEBUG [pym.test][Dummy-4] 1 None o 1
2014-03-16 14:09:23,361 DEBUG [pym.test][Dummy-3] 2 2 oo 2
2014-03-16 14:09:24,301 DEBUG [pym.test][Dummy-4] 3 None ooo 3
2014-03-16 14:09:25,419 DEBUG [pym.test][Dummy-3] 4 2 oooo 4

And this is part of my mixin:

    @declared_attr
    def editor_id(cls):
        """ID of user who was last editor."""
        return Column(
            Integer(),
            ForeignKey(
                "pym.user.id",
                onupdate="CASCADE",
                ondelete="RESTRICT"
            ),
            nullable=True,
            onupdate=_validate_editor
        )

The validator was intended to remind me to set editor_id on updates, but I 
had to deactivate it because of the above phenomenon:

def _validate_editor(context):
    pass
    # if not context.current_parameters['editor_id']:
    #     raise ValueError('Editor must be set on update.')


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