[Zope] q: How should I get a guaranteed unique id in Zope?

Chris Meyers chris@hddesign.com
Tue, 15 Jan 2002 08:49:37 -0600


On Tue, Jan 15, 2002 at 02:33:22AM -0500, Joe Block wrote:
> 
> The awkward thing is, when I create a job, I have to simultaneously 
> create a comment to attach to it with information about who initially 
> created the job, and some descriptive data, so I need to know what jobid 
> the job gets so I can add the comment to the comment db with the 
> appropriate jobid identifier.
> 
> I'm probably missing something in postgresql where I can do an insert 
> and have it return whatever jobid it automatically generates.  On the 
> Zope side, I'm using ZPsycoPg as my postgres connection.
> 
> jpb
> --
> Joe Block <jpb@ApesSeekingKnowledge.net>

You can create a function in PostgreSQL which will do the insert in the jobs table, and return the id it created. Something like this works for us in a similar situation:

DROP FUNCTION insert_person(text);

CREATE FUNCTION insert_person(text)
RETURNS int4
AS 'DECLARE
      id INT4;
    BEGIN
      id := nextval(\'tbl_People_idPeople_seq\');
      INSERT INTO "tbl_People (id,iduser)
        VALUES ( id, $1);
      RETURN id;
    END;'
LANGUAGE 'plpgsql'; 

Then in your ZSQLMethod, you would do something like this:

select insert_person(<dtml-sqlvar iduser type="string">);

HTH,
Chris