Skip to content

Changes #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions pysimplesql/pysimplesql.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,8 @@ def records_changed(self, recursive=True, column_name:str=None) -> bool:
logger.debug(f'Checking if records have changed in table "{self.table}"...')

# Virtual rows wills always be considered dirty
if self.get_current_row().virtual: return True
if self.rows:
if self.get_current_row().virtual: return True

dirty = False
# First check the current record to see if it's dirty
Expand Down Expand Up @@ -902,6 +903,10 @@ def insert_record(self, values:dict=None, skip_prompt_save=False) -> None:
else:
# At minimum, we should update the description column
new_values[self.description_column] = 'New Record'
# Make sure we take into account the foreign key relationships...
for r in self.frm.relationships:
if self.table == r.child_table and r.update_cascade:
new_values[r.fk_column] = self.frm[r.parent_table].get_current_pk()

# Update the pk to match the expected pk the driver would generate on insert.
new_values[self.pk_column] = self.driver.next_pk(self.table, self.pk_column)
Expand Down Expand Up @@ -961,6 +966,10 @@ def save_record(self, display_message=True, update_elements=True):

if val =='':
val = None

# Fix for Checkboxes switching from 0 to False, and from 1 to True
if type(val) is bool and type(self[v['column']]) is int:
val = int(val)

current_row[v['column']] = val

Expand All @@ -971,13 +980,13 @@ def save_record(self, display_message=True, update_elements=True):
return SAVE_NONE + SHOW_MESSAGE

# check to see if cascading-fk has changed before we update database
fk_changed = False
fk_column = self.frm.get_parent_cascade_fk(self.table)
if fk_column:
cascade_fk_changed = False
cascade_fk_column = self.frm.get_cascade_fk_column(self.table)
if cascade_fk_column:
# check if fk
for v in self.frm.element_map:
if v['query'] == self and pysimplesql.get_record_info(v['element'].Key)[1] == fk_column:
fk_changed = self.records_changed(recursive=False, column_name=v)
if v['query'] == self and pysimplesql.get_record_info(v['element'].Key)[1] == cascade_fk_column:
cascade_fk_changed = self.records_changed(recursive=False, column_name=v)

# Update the database from the stored rows
if self.transform is not None: self.transform(changed, TFORM_ENCODE)
Expand All @@ -1000,7 +1009,7 @@ def save_record(self, display_message=True, update_elements=True):
pk = self.get_current_pk()

# If child changes parent, move index back and requery/requery_dependents
if fk_changed: # TODO: Research why fk_changed is triggering at timems it does not need to
if cascade_fk_changed and not current_row.virtual: # Virtual rows already requery, and don't have any dependents.
self.frm[self.table].requery(select_first=False) #keep spot in table
self.frm[self.table].requery_dependents()

Expand Down Expand Up @@ -1415,15 +1424,15 @@ def get_parent(self, table):
return r.parent_table
return None

def get_parent_cascade_fk(self, table):
def get_cascade_fk_column(self, table):
"""
Return the parent fk that cascade-filters for the passed-in table
Return the cascade fk that filters for the passed-in table
:param table: The table (str) of child
:return: The name of the cascade-fk, or None
"""
for qry in self.queries:
for r in self.relationships:
if r.child_table == self[table].table:
if r.child_table == self[table].table and r.update_cascade:
return r.fk_column
return None

Expand Down