@@ -492,7 +492,8 @@ def records_changed(self, recursive=True, column_name:str=None) -> bool:
492
492
logger .debug (f'Checking if records have changed in table "{ self .table } "...' )
493
493
494
494
# Virtual rows wills always be considered dirty
495
- if self .get_current_row ().virtual : return True
495
+ if self .rows :
496
+ if self .get_current_row ().virtual : return True
496
497
497
498
dirty = False
498
499
# First check the current record to see if it's dirty
@@ -964,6 +965,10 @@ def insert_record(self, values:dict=None, skip_prompt_save=False) -> None:
964
965
else :
965
966
# At minimum, we should update the description column
966
967
new_values [self .description_column ] = 'New Record'
968
+ # Make sure we take into account the foreign key relationships...
969
+ for r in self .frm .relationships :
970
+ if self .table == r .child_table and r .update_cascade :
971
+ new_values [r .fk_column ] = self .frm [r .parent_table ].get_current_pk ()
967
972
968
973
# Update the pk to match the expected pk the driver would generate on insert.
969
974
new_values [self .pk_column ] = self .driver .next_pk (self .table , self .pk_column )
@@ -1027,6 +1032,10 @@ def save_record(self, display_message=True, update_elements=True) -> None:
1027
1032
1028
1033
if val == '' :
1029
1034
val = None
1035
+
1036
+ # Fix for Checkboxes switching from 0 to False, and from 1 to True
1037
+ if type (val ) is bool and type (self [v ['column' ]]) is int :
1038
+ val = int (val )
1030
1039
1031
1040
current_row [v ['column' ]] = val
1032
1041
@@ -1037,13 +1046,13 @@ def save_record(self, display_message=True, update_elements=True) -> None:
1037
1046
return SAVE_NONE + SHOW_MESSAGE
1038
1047
1039
1048
# check to see if cascading-fk has changed before we update database
1040
- fk_changed = False
1041
- fk_column = self .frm .get_parent_cascade_fk (self .table )
1042
- if fk_column :
1049
+ cascade_fk_changed = False
1050
+ cascade_fk_column = self .frm .get_cascade_fk_column (self .table )
1051
+ if cascade_fk_column :
1043
1052
# check if fk
1044
1053
for v in self .frm .element_map :
1045
- if v ['query' ] == self and pysimplesql .get_record_info (v ['element' ].Key )[1 ] == fk_column :
1046
- fk_changed = self .records_changed (recursive = False , column_name = v )
1054
+ if v ['query' ] == self and pysimplesql .get_record_info (v ['element' ].Key )[1 ] == cascade_fk_column :
1055
+ cascade_fk_changed = self .records_changed (recursive = False , column_name = v )
1047
1056
1048
1057
# Update the database from the stored rows
1049
1058
if self .transform is not None : self .transform (changed , TFORM_ENCODE )
@@ -1066,7 +1075,7 @@ def save_record(self, display_message=True, update_elements=True) -> None:
1066
1075
pk = self .get_current_pk ()
1067
1076
1068
1077
# If child changes parent, move index back and requery/requery_dependents
1069
- if fk_changed : # TODO: Research why fk_changed is triggering at timems it does not need to
1078
+ if cascade_fk_changed and not current_row . virtual : # Virtual rows already requery, and don't have any dependents.
1070
1079
self .frm [self .table ].requery (select_first = False ) #keep spot in table
1071
1080
self .frm [self .table ].requery_dependents ()
1072
1081
@@ -1485,15 +1494,15 @@ def get_parent(self, table):
1485
1494
return r .parent_table
1486
1495
return None
1487
1496
1488
- def get_parent_cascade_fk (self , table ):
1497
+ def get_cascade_fk_column (self , table ):
1489
1498
"""
1490
- Return the parent fk that cascade- filters for the passed-in table
1499
+ Return the cascade fk that filters for the passed-in table
1491
1500
:param table: The table (str) of child
1492
1501
:return: The name of the cascade-fk, or None
1493
1502
"""
1494
1503
for qry in self .queries :
1495
1504
for r in self .relationships :
1496
- if r .child_table == self [table ].table :
1505
+ if r .child_table == self [table ].table and r . update_cascade :
1497
1506
return r .fk_column
1498
1507
return None
1499
1508
0 commit comments