On Sep 25, 2:11 am, nkhalasi <khal...@gmail.com> wrote: > Hi, > > I followed the guidelines provided into this post to hook in my custom > ID generator. > -http://article.gmane.org/gmane.comp.python.sqlalchemy.user/3236/match... > However it does not get invoked upon each insert and hence gives me an > integrity error at runtime. > > def get_next_id(): > session = meta.Session() > query = session.query(PKGenerator).filter_by > (table_name='principals') > nxpkgen = query.first() > if nxpkgen: > nxid = nxpkgen.next_id > if nxid > nxpkgen.end_id: > raise Exception('Primary Key range has been exhausted') > nxpkgen.next_id += 1 > session .commit() > return nxid > else: > raise Exception('Next Primary Key could not be found') > > My entity's PK Column definition looks like this. > id = Column(bigint,Sequence('principals_seq_id',optional=True), > > primary_key=True,autoincrement=False,default=generator.get_next_id())
Leave off the () after get_next_id. You want to pass the function itself as the default, not the result of calling it once. Also, you may want to use locking in your get_next_id function if you can (either pessimistic or optimistic locking should work). -Conor > When I run my app, i get these > 12:18:18,078 INFO [sqlalchemy.engine.base.Engine.0x...be0c][945] > INSERT INTO principals (id, handle, name, type, subtype, created_on, > last_updated) VALUES (%s, %s, %s, %s, %s, CURRENT_TIMESTAMP, %s) > 12:18:18,078 INFO [sqlalchemy.engine.base.Engine.0x...be0c][946] > [12L, u'rpithadiya', 'Ritesh Pithadiya', 'user', u'user', None] > 12:18:18,079 INFO [sqlalchemy.engine.base.Engine.0x...be0c][945] > INSERT INTO principals (id, handle, name, type, subtype, created_on, > last_updated) VALUES (%s, %s, %s, %s, %s, CURRENT_TIMESTAMP, %s) > 12:18:18,079 INFO [sqlalchemy.engine.base.Engine.0x...be0c][946] > [12L, u'hdfcbank', 'HDFC Bank Ltd.', 'enterprise', u'bank', None] > 12:18:18,080 INFO [sqlalchemy.engine.base.Engine.0x...be0c][733] > ROLLBACK > 12:18:18,085 DEBUG [pluto.controllers.enterpriseusers][71] Problems > creating enterprise users - (IntegrityError) (1062, "Duplicate entry > '12' for key 1") > > Please advise if I am missing something here. > > Regards, > Naresh --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---