Re: Atomic value increment
On Jul 12, 2006, at 7:38 PM, Adrian Holovaty wrote: > > On 7/12/06, Jay Parlar <[EMAIL PROTECTED]> wrote: >> Is there a better way to do this? More specifically, an atomic way to >> generate and set the new unique_id? I can't just use the >> autogenerated >> 'id' primary key as an unique identifier, because I have a list of >> legacy unique_id values which I have to initially populate the db >> with. > > Use the auto-incrementing "id" field. Populating the DB with some > explicit values shouldn't pose any problems in that case. The only caveat is that the auto increment is not smart enough to skip over values that already exist (in Postgres anyway, sequences are simple number generators that know nothing about the table they generate values for). So you need to set the starting sequence value greater than the maximum value in the table, otherwise you'll get unique key constraint violations on insertions. If you're pre- populating the table with data in your sql initialization, at the end, make sure to prime the sequence using the setval function, like this: SELECT setval('mytable_seq_id', 999); In this case, 999 is the current maximum value, 1000 will be the next returned sequence id. Don --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: Atomic value increment
On 7/12/06, Jay Parlar <[EMAIL PROTECTED]> wrote: > Is there a better way to do this? More specifically, an atomic way to > generate and set the new unique_id? I can't just use the autogenerated > 'id' primary key as an unique identifier, because I have a list of > legacy unique_id values which I have to initially populate the db > with. Use the auto-incrementing "id" field. Populating the DB with some explicit values shouldn't pose any problems in that case. Adrian -- Adrian Holovaty holovaty.com | djangoproject.com --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: Atomic value increment
On 7/12/06, Ian Holsman <[EMAIL PROTECTED]> wrote: > > why not use a GUID ? > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/163604 That's pretty neat, but I do need the values to increase by one everytime. Jay P. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: Atomic value increment
why not use a GUID ? http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/163604 On 13/07/2006, at 8:08 AM, Jay Parlar wrote: > > On 7/12/06, Don Arbow <[EMAIL PROTECTED]> wrote: >> >> On Jul 12, 2006, at 8:56 AM, Jay Parlar wrote: >>> >>> Is there a better way to do this? More specifically, an atomic >>> way to >>> generate and set the new unique_id? I can't just use the >>> autogenerated >>> 'id' primary key as an unique identifier, because I have a list of >>> legacy unique_id values which I have to initially populate the db >>> with. >>> >> >> >> You could populate your database with the legacy values, then set the >> sequence start number to the number following those. >> >> You can do this in postgres with the setval function. >> > > > What about a table lock? My whole system is currently implemented with > the unique_id column, so it'd be much easier if I didn't have to > change that. > > The stuff that happens inside the view is minimal, so there'd be very > little performance loss if one process was waiting for the lock to > release. > > Jay P. > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: Atomic value increment
On 7/12/06, Don Arbow <[EMAIL PROTECTED]> wrote: > > On Jul 12, 2006, at 8:56 AM, Jay Parlar wrote: > > > > Is there a better way to do this? More specifically, an atomic way to > > generate and set the new unique_id? I can't just use the autogenerated > > 'id' primary key as an unique identifier, because I have a list of > > legacy unique_id values which I have to initially populate the db > > with. > > > > > You could populate your database with the legacy values, then set the > sequence start number to the number following those. > > You can do this in postgres with the setval function. > What about a table lock? My whole system is currently implemented with the unique_id column, so it'd be much easier if I didn't have to change that. The stuff that happens inside the view is minimal, so there'd be very little performance loss if one process was waiting for the lock to release. Jay P. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: Atomic value increment
On Jul 12, 2006, at 8:56 AM, Jay Parlar wrote: > > Is there a better way to do this? More specifically, an atomic way to > generate and set the new unique_id? I can't just use the autogenerated > 'id' primary key as an unique identifier, because I have a list of > legacy unique_id values which I have to initially populate the db > with. > You could populate your database with the legacy values, then set the sequence start number to the number following those. You can do this in postgres with the setval function. Don --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Atomic value increment
I've got a model something like this: class Product(models.Model) name = models.CharField(...) unique_id = models.PostiveIntegerField(unique=True) Instances of this model are only ever created programatically from a view. My current (and awful) scheme for getting a new unique_id for a new product is to simply get the highest current unique_id in the database, and add 1. That works well enough for testing with the dev server, but is sure to eventually fail when I've got multiple instances running via mod_python. Because of the 'unique=True', I know I don't have to worry about duplicate unique_id values going into the database, but the error that gets generated is... not the greatest. A ProgrammingError gets raised when a duplicate unique_id gets put it. Is there a better way to do this? More specifically, an atomic way to generate and set the new unique_id? I can't just use the autogenerated 'id' primary key as an unique identifier, because I have a list of legacy unique_id values which I have to initially populate the db with. Jay P. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---