Skip to content

Commit 7f159d8

Browse files
committed
refs #281, set_by_pk improvements
There were certain situations where set_by_pk would fail if the primary key was not found (like after deleting a virtual record.). This ensures that an index will be selected even if the pk value is not found
1 parent b84643d commit 7f159d8

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

pysimplesql/pysimplesql.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,7 @@ def set_by_pk(
13711371
:param omit_elements: (optional) A list of elements to omit from updating
13721372
:returns: None
13731373
"""
1374+
print(f"Setting pk to {pk}")
13741375
logger.debug(f"Setting table {self.table} record by primary key {pk}")
13751376
if omit_elements is None:
13761377
omit_elements = []
@@ -1384,15 +1385,12 @@ def set_by_pk(
13841385
# don't update self/dependents if we are going to below anyway
13851386
self.prompt_save(update_elements=False)
13861387

1387-
# find current index of pk in resorted rows (not in-place)
1388-
print(f"\nself.rows for {self.table}:\n", self.rows)
1389-
# for i, row in self.rows.iterrows():
1390-
# if row[self.pk_column] == pk:
1391-
# self.current_index = i
1392-
# break
1393-
self.current_index = (
1394-
self.rows.sort_index().index[self.rows[self.pk_column] == pk].tolist()[0]
1395-
)
1388+
# Move to the numerical index of where the primary key is located. If the pk
1389+
# value can't be found, move to the last index
1390+
idx = [i for i, value in enumerate(self.rows[self.pk_column]) if value == pk]
1391+
idx = idx[0] if idx else len(self.rows.index)
1392+
self.current_index = idx
1393+
13961394
if update_elements:
13971395
self.frm.update_elements(self.table, omit_elements=omit_elements)
13981396
if requery_dependents:

0 commit comments

Comments
 (0)