From: Tony Mack Date: Tue, 3 Jun 2008 02:25:07 +0000 (+0000) Subject: obtain integer primary keys by calling nextval() on the sequence instead of quering... X-Git-Tag: PLCAPI-4.3-1~34 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=b74c5345bb5c74c64beeeacad2e9aaba3220455a;p=plcapi.git obtain integer primary keys by calling nextval() on the sequence instead of quering the table's oids --- diff --git a/PLC/Table.py b/PLC/Table.py index 2362d61..74da3e9 100644 --- a/PLC/Table.py +++ b/PLC/Table.py @@ -1,4 +1,4 @@ -from types import StringTypes +from types import StringTypes, IntType, LongType import time import calendar @@ -230,6 +230,14 @@ class Row(dict): if not self.has_key(self.primary_key) or \ keys == [self.primary_key] or \ insert is True: + + # If primary key id is a serial int, get next id + if self.fields[self.primary_key].type in (IntType, LongType): + pk_id = self.api.db.next_id(self.table_name, self.primary_key) + self[self.primary_key] = pk_id + db_fields[self.primary_key] = pk_id + keys = db_fields.keys() + values = [self.api.db.param(key, value) for (key, value) in db_fields.items()] # Insert new row sql = "INSERT INTO %s (%s) VALUES (%s)" % \ (self.table_name, ", ".join(keys), ", ".join(values)) @@ -244,9 +252,6 @@ class Row(dict): self.api.db.do(sql, db_fields) - if not self.has_key(self.primary_key): - self[self.primary_key] = self.api.db.last_insert_id(self.table_name, self.primary_key) - if commit: self.api.db.commit()