Built in method to prevent deletion of rows with linked field

Is there any built in method in anvil to prevent deletion of rows with linked fields so that one does not get TableError.

3 Likes

Hi @alexri,

At the moment there isn’t a way to do this, but I like the suggestion. I’ve moved this to feature requests so we can gauge interest!

2 Likes

In SQL foreign-key terms, this would be an ON DELETE RESTRICT, as opposed to the current ON DELETE NO ACTION.

If you were to do this SQL-style, then the restriction would be declared at the point-of-reference, i.e., the column that links to the target, not at the target itself.

Another facet would be how to report such an attempt to the calling code.

  • Pythonic: raise a distinct Exception
  • C-style: return a distinct value.

In either case, it may be useful to know the row that “objected” to the delete. Otherwise, one might have a fairly long database search to find it.

I’m all in favor of exposing a few more SQL mechanisms. Specifying columns as unique would be another good one, along with restricting deletion of linked columns or cascading deletion.

There have already been steps in that direction. For example, you can mark columns to be indexed in three different ways.

For these more-expensive versions of update and delete, rather than impose the overhead on all existing calls in all apps, it may be wise to implement new functions, e.g., checked_update and checked_delete.

Yes, it means that the folks writing the calls will have to think more about what they’re doing. That’s not always a bad thing. :wink:

1 Like