Thanks Conor. My comments below.
On Sep 25, 9:05 pm, Conor <[email protected]> wrote:
> On Sep 25, 2:11 am, nkhalasi <[email protected]> wrote:
>
> 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
>
Thanks for the advise.
This is a very raw logic and what I really intend to do is a little
more complicated. And yes I will have to take care of locking and
synchronization.
Right now I am trying to figure out the process of hooking my custom
ID generator logic into the ORM layer.
I figured that I need to leave off () out quite late when I tried
sending my function as a ColumnDefault(custom_id_generator) instead of
the Sequence in the Column definition.
However now in that case my updates were either not getting committed
or the main transaction was getting closed, until I used the following
logic
def custom_id_generator(ctx):
from sqlalchemy import select
log.debug('generator.newid() invoked with context = %s' %ctx)
id = ctx.connection.execute(select([pkgt.c.next_id],
pkgt.c.table_name=='principals',
for_update=True)).scalar()
log.debug('Complied? %s, Autocommit? %s' %(ctx.compiled,
ctx.should_autocommit))
ctx.connection.execute(pkgt.update(values={pkgt.c.next_id :
pkgt.c.next_id +1}))
log.debug('Finished writing back the next id')
return id
With this function my transaction gets committed immediately after the
update. I am yet to figure out a way to disable that.
However I should try my original function by using it in
default=get_next_id.
Thanks for the help anyways.
Regards,
Naresh
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---