From be7b9b0936f35de21e745b87d222a63100959ff7 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 16 Apr 2024 11:45:34 +0300 Subject: Set InbredSet.InbredSetId value in a more robust way. --- qc_app/db/populations.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/qc_app/db/populations.py b/qc_app/db/populations.py index 06ae773..48ead2e 100644 --- a/qc_app/db/populations.py +++ b/qc_app/db/populations.py @@ -29,21 +29,24 @@ def populations_by_species(conn: mdb.Connection, speciesid) -> tuple: def save_population(conn: mdb.Connection, population_details: dict) -> dict: """Save the population details to the db.""" with conn.cursor(cursorclass=DictCursor) as cursor: - cursor.execute("SELECT MAX(Id) AS last_id FROM InbredSet") - new_id = cursor.fetchone()["last_id"] + 1 cursor.execute( "INSERT INTO InbredSet(" - "Id, InbredSetId, InbredSetName, Name, SpeciesId, FullName, " - "MenuOrderId, Description" + "InbredSetName, Name, SpeciesId, FullName, MenuOrderId, Description" ") " "VALUES (" - "%(Id)s, %(InbredSetId)s, %(InbredSetName)s, %(Name)s, " - "%(SpeciesId)s, %(FullName)s, %(MenuOrderId)s, %(Description)s" + "%(InbredSetName)s, %(Name)s, %(SpeciesId)s, %(FullName)s, " + "%(MenuOrderId)s, %(Description)s" ")", { - "Id": new_id, - "InbredSetId": new_id, "MenuOrderId": 0, **population_details }) - return {**population_details, "population_id": new_id} + new_id = cursor.lastrowid + cursor.execute("UPDATE InbredSet SET InbredSetId=%s WHERE Id=%s" + (new_id, new_id)) + return { + **population_details, + "Id": new_id, + "InbredSetId": new_id, + "population_id": new_id + } -- cgit v1.2.3