Problems using SessionContext in Functions Supplying Default Values

Issue #703 resolved
paj created an issue

I have a function that allocates primary keys to a table, using a custom method. I have set my table's primary key column to take its default from this function. Inside the function, it accesses a table, which servers a pseudo-sequence. I found that changes made to the data in the table row do not get propagated back to the database.

Presumably this happens because the default value is only fetched within the flush(), so the updated pseudo-sequence table doesn't get included in the flush. The problem is only really likely to occur when using SessionContext.

I've attached a test case.

Comments (2)

  1. Mike Bayer repo owner

    as mentioned on the list, the recommended pattern here is this:

    seq = Table('seq', m,
             Column('seq', String(50), primary_key=True),
             Column('nextval', Integer))
    
    def newid(ctx):
         id = ctx.connection.execute(select([seq.c.nextval](seq.c.nextval),  
    seq.c.seq=='tbl', for_update=True)).scalar()
         ctx.connection.execute(seq.update(values=[:  
    seq.c.nextval +1](seq.c.nextval)))
         return hex(id)
    

    8f17efd7a3c337045299927f1a30cbbd013dd6b1 adds some extra logic to ensure that context.connection is freely usable within default generator functions.

  2. Log in to comment