String column with Sequence breaks identity map

Issue #782 resolved
Former user created an issue

I'm trying to create a class with an id of type 'str' that autoincrements by default (e.g. '1', '2', etc). I gave its corresponding table a String column with an associated Sequence.

If I save an instance, its id is populated with a long, but if I then retrieve the instance from the database, an object with an id of type 'str' is (correctly) returned. This causes the session's identity map lookup to fail.

Comments (1)

  1. Mike Bayer repo owner

    sequences are often executed "inline" with a SQL INSERT statement (as of 0.4), so any python-side conversion to string is not portable (also, sequences are specifically for integers so i dont favor any new bells and whistles within Sequence). instead, do the conversion yourself with a pre-executed default (0.4 example):

    def mydefault(ctx):
        conn = ctx.connection
        next = conn.execute(Sequence('my_sequence'))
        return str(next)
    
    t = Table('mytable', meta, 
        Column('my_sequence_col', String(30), default=mydefault, primary_key=True)
    )
    
  2. Log in to comment