Skip to content
Browse files

Retrieving the object ID when the DB sets it on insert.

Doing this for all tables with a random INTEGER PRIMARY KEY.

Signed-off-by: Joe Baker <jbaker0428@gmail.com>
  • Loading branch information...
1 parent 15a0aa0 commit 89ebf36a25462866c530f8cf98bcc9bbfe13c8a6 @jbaker0428 committed Apr 22, 2012
Showing with 13 additions and 0 deletions.
  1. +13 −0 gc_attendance.py
View
13 gc_attendance.py
@@ -1092,6 +1092,10 @@ def insert(self, db=gcdb, connection=None):
params = (self.name, self.semester.name,)
cur.execute('INSERT INTO groups VALUES (NULL,?,?)', params)
+ cur.execute('SELECT id FROM groups WHERE name=? AND value=?', params)
+ row = cur.selectone()
+ if row is not None:
+ self.id = row[0] # Retrieve the ID as set by the database
finally:
cur.close()
@@ -1540,6 +1544,11 @@ def insert(self, db=gcdb, connection=None):
params = (isoformat(self.excuse_dt), self.event.id, self.reason, self.student.rfid,)
# INSERTing 'NULL' for the integer primary key column autogenerates an id
cur.execute('INSERT INTO excuses VALUES (NULL,?,?,?,?)', params)
+ params = (isoformat(self.excuse_dt), self.event.id, self.student.rfid,)
+ cur.execute('SELECT id FROM excuses WHERE dt=? AND event=? AND student=?', params)
+ row = cur.selectone()
+ if row is not None:
+ self.id = row[0] # Retrieve the ID as set by the database
finally:
cur.close()
@@ -2059,6 +2068,10 @@ def insert(self, db=gcdb, connection=None):
params = (self.name, isoformat(self.event_dt), self.event_type, self.group.id,)
cur.execute('INSERT INTO events VALUES (NULL,?,?,?,?)', params)
+ cur.execute('SELECT id FROM events WHERE eventname=? AND dt=? AND type=? AND group=?', params)
+ row = cur.selectone()
+ if row is not None:
+ self.id = row[0] # Retrieve the ID as set by the database
finally:
cur.close()

0 comments on commit 89ebf36

Please sign in to comment.
Something went wrong with that request. Please try again.