[sqlalchemy] Auto-incrementing attribute with custom logic
Hi everyone! I have a simple Invoices class with a Number attribute that has to be filled in by the application when the user saves an invoice. There are some constraints: 1) the application is a (thin) client-server one, so whatever determines the number must look out for collisions 2) Invoices has a version attribute too, so I can't use a simple DBMS-level autoincrementing field I'm trying to build this using a custom Type that would kick in every time an invoice gets saved. Whenever process_bind_param is called with a None value, it will call a singleton of some sort to determine the number and avoid collisions. Is this a decent solution? Anyway, I'm having a problem.. Here's my custom Type: class AutoIncrement(types.TypeDecorator): impl = types.Unicode def copy(self): return AutoIncrement() def process_bind_param(self, value, dialect): if not value: # Must find next autoincrement value value = 1 # Test value :) return value My problem right now is that when I save an Invoice and AutoIncrement sets 1 as value for its number, the Invoice instance *doesn't* get updated with the new number.. Is this expected? Am I missing something? Many thanks for your time! (SQLA 0.5.3 on Python 2.6, using postgreSQL 8.3) --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] auto incrementing
Hello, I was wondering if anybody has a good strategy for auto incrementing fields. I want to auto increment field called case# . I have a choice of database auto increment on field case# or do it myself? (correct? No other choices exists? or something in between?) 1. I would like to be able to do pick a number where we will start doing a case#? 2. Reserver a case# for a special group which can auto increment case# between 2,000,000-2,999,999, and add them as they come. 3. I don't want to use (system_id) So it seems as the only way is to make my primary key: case# - unique key, primary, not auto incrementing and let some program manage auto incrementing. What options do I have with sqlalchemy to manage any range of these primary keys? 1. let db auto increment 2. Hold the next case# in a separate database table, and let my program use it to find next case# value. How would I lock/unlock the next case# to make there is no race condition and each case# is taken/successfully saved. 3. Any other options? Have people exeperienced with other strategy that is semi-automatic, and would for for these cases.? Thanks, Lucas --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---