Ready to get started?

Join Adocasts Plus for $8/mo, or sign into an existing Adocasts Plus account, to get access to all of our lessons.

robot mascot smiling

Replacing A Course's Deleted Difficulty

@tomgobich
Published by
@tomgobich
In This Lesson

We'll add the ability to gracefully handle instances where a required relationship's record is being deleted. When a difficulty is being used by a course, we'll have our users select a replacement difficulty for the one being deleted.

Chapters

00:00 - Gracefully Deleting A Not Null Foreign Key Relationship
00:56 - Creating An Example of our Use-Case
03:30 - Adding $extras to our DTOs as Meta
05:31 - Adding a Replacement Select to the Delete Confirmation Dialog
07:42 - Adding Form Data to the Delete Confirmation Dialog
09:07 - Computing the Replacement Options
09:44 - Adding a Difficulty Destroy Validator
10:46 - Passing Meta Data Into Our Validator
12:11 - Validating the Destroy Action
12:50 - Replacing the Difficulty Before Deleting It
14:42 - Adding Select Support to our FormInput Component
15:54 - Testing our Delete Replacement Flow

Join the Discussion 2 comments

Create a free account to join in on the discussion
  1. @aaron-ford

    Is AdonisJS set up to handle soft deletes? If you didn't want to replace the difficulty and maintain the original relationships? Or if you wanted to delete a course or organization for some reason, and not lose any records you might have to things like users grades?

    Also, I feel like I've occasionally seen warnings in the code that I think are due to FormInput type group (remember me on the login page) not having a label or a v-model attribute. Should a group type have those attributes? Or should they be made optional in the FormInput's defined props?

    1
    1. Responding to aaron-ford
      @tomgobich

      Hi Aaron! Unfortunately, not out of the box. Looks like there is a community package to add support for it, though. An alternative approach I've seen discussed is to have a soft_deletes table that contains a serialized version of the deleted row with additional columns specifying its original table & id. You'd then delete the record, serialize it, then add it to the soft_deletes table so that you can still grab it and restore it if needed. I've never attempted this approach, but it does sound interesting, so I thought I'd share. Not sure how this would handle foreign keys pointing to the deleted id though.

      Yes, sorry about that, those should be optional props!

      0