Can't find data record in database table context

Can't find data record in database table context

by Lindsay Hayward -
Number of replies: 2

By human error, we accidentally triggered a deletion job on our Spring 2021 parent course category, sub-categories and courses (from the category/course management page). We are on Moodle 3.7.3

The deletion job removed the first 5-6 course sub-categories AND their nested courses before we "stopped the bleeding" by shutting down and restarting two of our web servers that we were on around the time the deletion was started. 

Now we are seeing an "Can't find data record in database table context" error message in three places:

  1. When trying to view the categories and course management page
  2. When trying to perform an import
  3. Step 2 (destination) when trying to perform a restore

Here is debugging (developer) information when trying to view the categories and course management page:

Can't find data record in database table context.

Debug info: SELECT * FROM {context} WHERE id = ?
[array (
0 => '1807519',
)]
Error code: invalidrecord

Stack trace:
line 1562 of /lib/dml/moodle_database.php: dml_missing_record_exception thrown
line 1538 of /lib/dml/moodle_database.php: call to moodle_database->get_record_select()
line 5191 of /lib/accesslib.php: call to moodle_database->get_record()
line 5577 of /lib/accesslib.php: call to context::instance_by_id()
line 5430 of /lib/accesslib.php: call to context->get_parent_context()
line 5008 of /lib/accesslib.php: call to context->is_locked()
line 484 of /lib/accesslib.php: call to context->get()
line 2288 of /course/lib.php: call to has_capability()
line 1886 of /course/classes/category.php: call to can_delete_course()
line 247 of /course/classes/management/helper.php: call to core_course_category->can_delete_full()
line 258 of /course/classes/management_renderer.php: call to core_course\management\helper::get_category_listitem_actions()
line 164 of /course/classes/management_renderer.php: call to core_course_management_renderer->category_listitem()
line 496 of /course/management.php: call to core_course_management_renderer->category_listing()

Things we've tried:

  • Running cronjobs several times
  • Clicking "Run Now" on the cleanup and context related tasks in the scheduled tasks area
  • Using API webservices to provision new categories to replace the ones that got deleted.
  • Tried deletion a sub-category of an old "archived" term to see if it could trigger contexts to get cleaned up

It's important to note that our term-based categories, sub-categories and courses are created normally through a provisioning process from our SIS system - fed through our database to create the categories, courses and finally, feed over enrollments.

Are there any other solutions we can try?

 

 


Average of ratings: Useful (1)
In reply to Lindsay Hayward

Re: Can't find data record in database table context

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I'm sure I don't need to tell you that "pulling the plug" out like you seem to have done was a big risk for corrupting your database.

I think you have two choices...
- restore from the latest good backup I hope you have
- go through and repair the database problems by hand (good luck)!

I prefer the first one.
In reply to Lindsay Hayward

Re: Can't find data record in database table context

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

I agree with Howard except to suggest attempting to restore the contexts by dropping the mdl_context table then restoring just this table from a recent (< 24 hours before the accident) backup. If you're using MySQL or MariaDB this should be possible. If you first restore this database to another temporary location you can check if this contains the missing id values (1807519, etc.).

If there have been subsequent changes to the site then this restored table may be missing some new contexts. You could modify the backup before restoring it to add any new records. Again, assuming MySQL/MariaDB, then the backups are text files that can be edited so you could take bulk of the mdl_context rows from the pre-accident backup and add any additional rows from a backup of the "broken" site, and restore from this combined file.

This is to specifically fix the "Can't find data record in database table context" errors, you may find there are other issues that need repairing.

Average of ratings: Useful (2)