FYI: DB table rows are not copyable

What I tried to do:
I had cached several selected db table rows in an object, containing data for UI use. Data for Data Binding was extracted from these rows, for user editing.

The idea was, once the user approved their changes to their data, the rows that would need to be updated were right there, ready to receive the new values.

Of course, as a matter of practice, I made sure that the UI only worked with a working copy of the data. If the user cancelled the changes, the working copy would simply be discarded, leaving the original data untouched.

I created the working copy via deepcopy().

What I’ve tried and what’s not working:
deepcopy() on the object.

It turns out, you can’t deepcopy() a class instance that contains a database table row. (Probably can’t deepcopy() anything containing the row. I’ll assume so, until proven otherwise.)

But that’s not a big problem. After all there’s a …

Simple workaround:
Instead of cacheing a reference to the row itself, just cache the row’s id. It’s just a string, and can be safely deepcopy()ed. Yes, when you need the actual row, there’s an extra step to retrieve it. But it shouldn’t stop anyone from applying a deepcopy-based “Save/Cancel” idiom.

1 Like