[Twisted-Python] enterprise.row

Dave Peticolas dave at krondo.com
Mon Apr 7 04:42:08 EDT 2003

On Mon, 2003-04-07 at 00:44, Justin Ryan wrote:
> > What I have done with postgres is to generate the primay key using
> > the nextval function on the sequence used for the serial column, setting
> > the row object's primary key, and then inserting it. This makes the t.e
> > code happy and you often need to know the new key anyway.
> Could you post the code that does this, or a simple example of how you
> are doing it?

One strategy I used was to subclass SQLReflector for postgres
databases like so:

class PGSQLReflector(SQLReflector):

    def newRow(self, row_class, template=None):
        return self.runInteraction(self._newRow, row_class, template)

    def _newRow(self, transaction, row_class, template=None):
        row = row_class()

        if template is not None:
            for column, _ in row_class.rowColumns:
                if (hasattr(template, column) and
                    not getKeyColumn(row_class, column)):
                    setattr(row, column, getattr(template, column))

        for key, _ in row_class.rowKeyColumns:
            sql = "SELECT NEXTVAL('%s');" % row_class.keyGenerators[key]
            val = transaction.fetchone()[0]
            row.assignKeyAttr(key, long(val))

        return row

This requires that you specify the names of the sequences used
to generate the keys in the row class. Obviously this code isn't
very general, since it assumes keys will always be generated by 

For your extensions which allow defaults, it might be better
to insert the row and then use the 'lastval' function to find
out what key was assigned to the row.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20030407/f1d0b846/attachment.pgp 

More information about the Twisted-Python mailing list