On Apr 13, 12:23 pm, "Koen Bok" <[EMAIL PROTECTED]> wrote:
> Ok, I'd rather handle it on the database level. Is that just a matter
> of creating a function and calling it on insert?

You need a sequence that has locks and rolls back on rollback.
Simplest way is to use a table: CREATE TABLE sequences (name
varchar(100) not null primary key, value int not null default 0);
and a stored proc to get the next id, in postgresql that would be
CREATE FUNCTION next_id(varchar) RETURNS int AS $$
    DECLARE id int;
    BEGIN
        UPDATE sequences SET value = value + 1 WHERE name = $1;
        SELECT value INTO id FROM sequences WHERE name = $1;
        RETURN id;
    END
$$ LANGUAGE plpgsql;

Then say you have table bar, you initialize the sequence:
INSERT INTO sequences ('bar', 0);
And create the table:
CREATE TABLE bar (bar_id int NOT NULL DEFAULT next_id('bar') PRIMARY
KEY, ...);
or in SA:
Table('bar',
    Column('id', Integer, primary_key=True,
default=func.next_id('bar')), ...
)

Ants


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

Reply via email to