Debug info: Subquery returns more than 1 row

Debug info: Subquery returns more than 1 row

Mohd SaroshMahendru
Number of replies: 4

Whenever I import a questions file into the Question Bank or add questions manually and navigate to the Questions tab to edit them, I encounter an error "Subquery returns more than 1 row" as shown in image. Upon investigating the database, I found that there are duplicate records with the same question ID, which should not be the case.

When I attempt to delete one of the duplicate entries directly from the database, I receive another error indicating a "Coding error detected", as shown in the attached image.

Moodle Version: 4.2.5

Please help with this.


Subquery returns more than 1 row Error:


Error reading from database
More information about this error

Debug info: Subquery returns more than 1 row

SELECT allversions.id AS versionid,
allversions.version,
allversions.questionid

FROM ft_question_versions allversions

WHERE allversions.questionbankentryid = (
SELECT givenversion.questionbankentryid
FROM ft_question_versions givenversion
WHERE givenversion.questionid = ?
)
AND allversions.status <> ?

ORDER BY allversions.version DESC

[array (
0 => 2687,
1 => 'draft',
)]
Error code: dmlreadexception


Stack trace:

  • line 494 of /lib/dml/moodle_database.php: dml_read_exception thrown
  • line 293 of /lib/dml/moodle_read_slave_trait.php: call to moodle_database->query_end()
  • line 1375 of /lib/dml/mysqli_native_moodle_database.php: call to mysqli_native_moodle_database->query_end()
  • line 63 of /mod/quiz/classes/question/bank/qbank_helper.php: call to mysqli_native_moodle_database->get_records_sql()
  • line 787 of /mod/quiz/classes/structure.php: call to mod_quiz\question\bank\qbank_helper::get_version_options()
  • line 779 of /mod/quiz/classes/output/edit_renderer.php: call to mod_quiz\structure->get_version_choices_for_slot()
  • line 549 of /mod/quiz/classes/output/edit_renderer.php: call to mod_quiz\output\edit_renderer->question()
  • line 522 of /mod/quiz/classes/output/edit_renderer.php: call to mod_quiz\output\edit_renderer->question_row()
  • line 94 of /mod/quiz/classes/output/edit_renderer.php: call to mod_quiz\output\edit_renderer->questions_in_section()
  • line 208 of /mod/quiz/edit.php: call to mod_quiz\output\edit_renderer->edit_page()


Attachment Screenshot from 2025-04-13 12-48-44.png
Attachment Screenshot from 2025-04-13 12-52-39.png
평균 등급 : -
In reply to Mohd SaroshMahendru

Debug info: Subquery returns more than 1 row

Tim Hunt
Core developers 사진 Documentation writers 사진 Particularly helpful Moodlers 사진 Peer reviewers 사진 Plugin developers 사진
This definitely should not be happening. The values in the questionid column of the question_versions table should be unique. (Why doesn't the DB schema enforce this? I created MDL-85197.)

In the cases where you have several rows in question_versions with the questionid, which questions are those? can deduce how it got like that?
In reply to Tim Hunt

Debug info: Subquery returns more than 1 row

Mohd SaroshMahendru
Same, I also have a question regarding why the database isn't enforcing constraints in this case. We have other Moodle platforms where everything works fine, but on this specific Moodle instance, we're seeing duplication of MCQ questions.

I've attached an image for reference — if you look closely, the same question ID is associated with two different ownerids. Additionally, one of the entries doesn't have any category associated with it. One is 638, which belonged to a student whose account was later deleted automatically after we imported a new users file (to update usernames). The other ownerid is 526, which belongs to the admin who actually created the question.

Attachment Screenshot from 2025-04-15 16-25-28.png
In reply to Mohd SaroshMahendru

Debug info: Subquery returns more than 1 row

Mohd SaroshMahendru
To solve the error, I edited the /mod/quiz/classes/question/bank/qbank_helper.php file to fetch only one question instead of two from the subquery.

Below is the query:
SELECT allversions.id AS versionid,
allversions.version,
allversions.questionid
FROM {question_versions} allversions
JOIN (
SELECT givenversion.questionbankentryid
FROM {question_versions} givenversion
WHERE givenversion.questionid = ?
ORDER BY givenversion.questionid DESC  -- to fetch the latest version of questionid created by admin
LIMIT 1
) subq ON allversions.questionbankentryid = subq.questionbankentryid
WHERE allversions.status ?
ORDER BY allversions.version DESC

Although this solved the error. But later for very few questions I am getting new error as shown below.

Coding error detected, it must be fixed by a programmer: Invalid context id specified context::instance_by_id()

More information about this error

Debug info:
Error code: codingerror×Dismiss this notification
Stack trace:
line 475 of /lib/classes/context.php: coding_exception thrown
line 965 of /lib/questionlib.php: call to core\context::instance_by_id()
line 1037 of /lib/questionlib.php: call to _tidy_question()
line 125 of /question/bank/editquestion/question.php: call to get_question_options()

The above error states that for some questions there are no contextid mapped and that's why this error shows up as per my understanding.

In reply to Mohd SaroshMahendru

Debug info: Subquery returns more than 1 row

Mohd SaroshMahendru

After completing all the previous steps, we recently encountered a new error while attempting quiz:

"Coding error detected, it must be fixed by a programmer: This question is of a type that is not installed on your system. No processing is possible. More information about this error."

Is there a way to resolve the multiple-choice question (MCQ) issues directly from the database? As mentioned in earlier messages, the duplication of questions is causing these errors.

To fix this, I need to remove the duplicate questions from the database where the contextid is NULL. However, I want to ensure that I remove all related duplicate data without causing further issues. Could you please guide me on which tables I should refer to in order to safely clean up these entries?

This is critical for us.