aboutsummaryrefslogtreecommitdiff
path: root/gn3
diff options
context:
space:
mode:
authorBonfaceKilz2021-12-22 14:03:27 +0300
committerBonfaceKilz2022-01-04 17:04:24 +0300
commit6bb51023032281f28736a4c761fa50e3072327e1 (patch)
treefa71bd005e11cc71ec0509077aefb55b7f76c0bf /gn3
parent27d54257735156ebc9d12f3ce638ea469d14e15e (diff)
downloadgenenetwork3-6bb51023032281f28736a4c761fa50e3072327e1.tar.gz
traits.py Delete a record only if it exists
In the case when the user tries to delete the same data twice, prior to this commit, an error was being generated. This commit remedies this by checking if a record exists prior to deleting it.
Diffstat (limited to 'gn3')
-rw-r--r--gn3/db/traits.py39
1 files changed, 22 insertions, 17 deletions
diff --git a/gn3/db/traits.py b/gn3/db/traits.py
index 68b6059..9d5ddd1 100644
--- a/gn3/db/traits.py
+++ b/gn3/db/traits.py
@@ -203,25 +203,30 @@ def delete_sample_data(conn: Any,
"AND Strain.Name = \"%s\"") % (trait_name,
phenotype_id,
str(strain_name)))
- strain_id, data_id = cursor.fetchone()
- cursor.execute(("DELETE FROM PublishData "
+ # Check if it exists if the data was already deleted:
+ if _result := cursor.fetchone():
+ strain_id, data_id = _result
+
+ # Only run if the strain_id and data_id exist
+ if strain_id and data_id:
+ cursor.execute(("DELETE FROM PublishData "
"WHERE StrainId = %s AND Id = %s")
- % (strain_id, data_id))
- deleted_published_data = cursor.rowcount
-
- # Delete the PublishSE table
- cursor.execute(("DELETE FROM PublishSE "
- "WHERE StrainId = %s AND DataId = %s") %
- (strain_id, data_id))
- deleted_se_data = cursor.rowcount
-
- # Delete the NStrain table
- cursor.execute(("DELETE FROM NStrain "
- "WHERE StrainId = %s AND DataId = %s" %
- (strain_id, data_id)))
- deleted_n_strains = cursor.rowcount
- except Exception as e: #pylint: disable=[C0103, W0612]
+ % (strain_id, data_id))
+ deleted_published_data = cursor.rowcount
+
+ # Delete the PublishSE table
+ cursor.execute(("DELETE FROM PublishSE "
+ "WHERE StrainId = %s AND DataId = %s") %
+ (strain_id, data_id))
+ deleted_se_data = cursor.rowcount
+
+ # Delete the NStrain table
+ cursor.execute(("DELETE FROM NStrain "
+ "WHERE StrainId = %s AND DataId = %s" %
+ (strain_id, data_id)))
+ deleted_n_strains = cursor.rowcount
+ except Exception as e: #pylint: disable=[C0103, W0612]
conn.rollback()
raise MySQLdb.Error
conn.commit()