Moodle database error

Moodle database error

by Nicolás Cabello -
Number of replies: 8

Hi to all, i am having some troubles when i try certain actions like removing old users, in some cases i get this:

No se puede encontrar registro de datos en la tabla course de la base de datos. (cant find entries in the table course of the database)

This is what i get in debugging:

Debug info: SELECT id,category FROM {course} WHERE id = ?

[array (
0 => '127',
)]
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 6827 of /lib/accesslib.php: call to moodle_database->get_record()
  • line 2093 of /lib/enrollib.php: call to context_course::instance()
  • line 2648 of /lib/enrollib.php: call to enrol_plugin->unenrol_user()
  • line 1058 of /lib/enrollib.php: call to enrol_plugin->user_delete()
  • line 4207 of /lib/moodlelib.php: call to enrol_user_delete()
  • line 29 of /admin/user/user_bulk_delete.php: call to delete_user()
Any help would be much apreciated
Average of ratings: -
In reply to Nicolás Cabello

Re: Moodle database error

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers
In reply to Leon Stringer

Re: Moodle database error

by Nicolás Cabello -
Hi, version Moodle 3.7.2+
Running on centos 7
In reply to Nicolás Cabello

Re: Moodle database error

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

Somehow a course has been deleted (ID 127) but there are still user enrolments for that course.

Firstly I would try updating the site to Moodle 3.7.9 as 3.7.2+ is missing 14 months of fixes and security updates. It's possible there's been a change which would stop this error.

If updating Moodle doesn't resolve this then you may have to manually alter the database, e.g.:

1. Back up the Moodle database.

2. Run the following statements (assuming you're using the default CFG->prefix of 'mdl_'.):

DELETE FROM mdl_enrol WHERE courseid NOT IN (SELECT id FROM mdl_course);
DELETE FROM mdl_user_enrolments WHERE enrolid NOT IN (SELECT id FROM mdl_enrol);
Average of ratings: Useful (2)
In reply to Leon Stringer

Re: Moodle database error

by Nicolás Cabello -
HI Leon thank you very much for taking the time. I tried the commands individually but there is no output. When i try both at once i get mysql sintax error.

Failed to execute SQL : SQL DELETE FROM mdl_enrol WHERE courseid NOT IN (SELECT id FROM mdl_course); DELETE FROM mdl_user_enrolments WHERE enrolid NOT IN (SELECT id FROM mdl_enrol); failed : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELETE FROM mdl_user_enrolments WHERE enrolid NOT IN (SELECT id FROM mdl_enrol)' at line 1
In reply to Nicolás Cabello

Re: Moodle database error

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

How are you running these? There shouldn't be much output just "n rows affected" showing the number of rows deleted.

If I run this in the mysql shell:

MariaDB> DELETE FROM mdl_enrol WHERE courseid NOT IN (SELECT id FROM mdl_course);  Query OK, 1 row affected (0.011 sec)

MariaDB> DELETE FROM mdl_user_enrolments WHERE enrolid NOT IN (SELECT id FROM mdl_enrol);  Query OK, 1 row affected (0.011 sec

Or you can run it in the SQL tab in phpMyAdmin. If it's still not working, please share a screenshot of how the statement is being added and any resulting error.

In reply to Leon Stringer

Re: Moodle database error

by Nicolás Cabello -
I am sorry I didnt mention another error. When a new user logs in, they see an error the first time. In debuggin this iit what it shows:

invalid parameter detected
File: /lib/externallib.php
Line: 332
Missing required key in single structure: classification
Error code: invalidparameter
* line 332 of /lib/externallib.php: invalid_parameter_exception thrown
* line 223 of /lib/externallib.php: call to external_api::validate_parameters()
* line 59 of /lib/ajax/service.php: call to external_api::call_external_function()

Debug info:
Error code: generalexceptionmessage
×Stack trace:
line 95 of /my/lib.php: Error thrown
line 127 of /my/index.php: call to my_copy_page()
In reply to Nicolás Cabello

Re: Moodle database error

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

This error looks unrelated to the database error so I suggest starting a new discussion thread with a relevant subject so that people who may know about this problem can help. If you share a screenshot of the error this will help us understand what you're seeing, hide or remove any information in the screenshot that you don't want to share on the Internet.

If you haven't already, please update to Moodle 3.7.9 first because again this problem could be resolved.

You can also search the forums, for example:

  1. Someone had this problem after changing permissions in Moodle.
  2. Someone had this problem when they had some missing blocks.


Average of ratings: Useful (1)
In reply to Leon Stringer

Re: Moodle database error

by Nicolás Cabello -
Hi Leon, thanks for all your help. I type the commands correctly now and it works fine, now i can delete the users. Thank you very much

  i am on the process of upgrading moodle, but probably i'll end up reinstalling the os.

Thank you for your help