My comments below.

On Sep 25, 9:05 pm, Conor <conor.edward.da...@gmail.com> wrote:
> On Sep 25, 2:11 am, nkhalasi <khal...@gmail.com> wrote:

> > 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')
>
> Leave off the () after get_next_id. You want to pass the function
> itself as the default, not the result of calling it once.
>

In the above function
1) if I do a commit, it commits the main transaction and subsequent
commit()s  fail complaining that the transaction is already closed.
2) if I do not commit, the next_id value is not updated resulting in
next run to use the IDs that were already used in the last run.
3) I tried session.begin_nested as well but that also did not result
into commiting my next_id and only did a release save point. I guess
this should be attributed to MySQL's behavior.

I am wondering how would I commit my ID range/next_id independent of
my regular application object commits? This is important for me
because I wanted to ultimately implement a variation of ID generator
like Sybase so that autoincrement happens in memory for the duration
of key cache size and then reset the memory counters to next key cache
sizes.
Here is my table :
pk_generator_table = Table('pk_generator', meta.metadata,
                        Column('table_name', types.CHAR(30),
primary_key=True, nullable=False),
                        Column('next_id', bigint, nullable=False),
                        Column('last_id', bigint, nullable=False),
                        Column('cache_size', types.Integer,
nullable=False),
                        mysql_engine='InnoDB',
                        mysql_row_format='DYNAMIC',
                        mysql_charset='utf8'
                    )


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

Reply via email to