Duplicate entry when starting a quiz attempt

Duplicate entry when starting a quiz attempt

by Martin Greenaway -
Number of replies: 5

Hi there, 

I have the following issue in Moodle 2.7:

Error writing to database
Debug info: Duplicate entry '2073-1' for key 'mdl_quesatte_queslo_uix'
INSERT INTO mdl_question_attempts (questionusageid,slot,behaviour,questionid,variant,maxmark,minfraction,maxfraction,flagged,questionsummary,rightanswer,responsesummary,timemodified) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)
[array (
0 => 2073,
1 => 1,
2 => 'deferredfeedback',
3 => '363',
4 => 1,
5 => '1.0000000',
6 => 0,
7 => 1,
8 => 0,
9 => 'When may fit adult ewes need worming?: Lambing; Weaning; Tupping; All of the above',
10 => 'Lambing',
11 => NULL,
12 => 1487081616,
)]
Error code: dmlwriteexception
Stack trace:
  • line 448 of /lib/dml/moodle_database.php: dml_write_exception thrown
  • line 1178 of /lib/dml/mysqli_native_moodle_database.php: call to moodle_database->query_end()
  • line 1224 of /lib/dml/mysqli_native_moodle_database.php: call to mysqli_native_moodle_database->insert_record_raw()
  • line 120 of /question/engine/datalib.php: call to mysqli_native_moodle_database->insert_record()
  • line 90 of /question/engine/datalib.php: call to question_engine_data_mapper->insert_question_attempt()
  • line 100 of /question/engine/lib.php: call to question_engine_data_mapper->insert_questions_usage_by_activity()
  • line 289 of /mod/quiz/locallib.php: call to question_engine::save_questions_usage_by_activity()
  • line 185 of /mod/quiz/startattempt.php: call to quiz_attempt_save_started()

This appears to be happening because the counter used to store the values for "mdl_quiz_attempts.uniqueid" and, related, entries in "mdl_question_attemts.questionusageid" is out of sync.  For instance, it's trying to create an entry here with value of 2073, but the highest in the database at the moment is 2076.

The indexes appear to be correctly configured in the database (MySQL, InnoDB).

I have some questions for anyone who understands the quiz process better than me - 

  1. Does the question attempt get created before the associated quiz attempt? If so, why? The unique constraint on mdl_quiz_attempts.uniqueid should have caught this already but it hasn't.  I can only assume we're creating "child" records before "parent" which is ... strange.
  2. Where is this unique ID managed from?
  3. Has anyone else seen this kind of issue?  I note some examples from some years ago of a similar error message caused by "orphaned data" on the migration from 1.9 -> 2.x but this is a server which has always been in the 2.7.x version family.
  4. Might this be something missing in my migration process - this appears to have happened on our staging and development servers, and not (at least not that I have been able to reproduce) on our production server.  I wonder if the tracking of the quiz attempts unique ID is carried over from previous attempts done on that server, and when we copy over the new prod database (with obviously more records in it) we get this mismatch, maybe?
Average of ratings: -
In reply to Martin Greenaway

Re: Duplicate entry when starting a quiz attempt

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

The counter being out of synch is really bad. I don't know how that could happen. You really need to find that out.

The counters/unique ids should be entirely handled by the database.

The most likely problem is when you copied from production to staging. Did you copy the counter values as well as the table contents?

In reply to Tim Hunt

Re: Duplicate entry when starting a quiz attempt

by Martin Greenaway -

Hi Tim, thanks for taking the time to respond.  I am a bit concerned about this obviously, because until I understand why it's happened I don't know that it's not going to happen on the production database, and I also don't know whether it's invalidating my backup strategy.  

It's entirely possible I'm not copying the counters correctly.  The backup script I'm using is a modification of an old script which does the following MySQL dump:

mysqldump -h $mysqldbhost $mysqlopt "$moodledb" | gzip > "$dbdump" || exit $?

The contents of $mysqlopt are as follows:

--default-character-set=utf8 --user=[user] --password=[password] --routines --triggers

The output does include AUTO_INCREMENT=(relevant number) for the AUTO_INCREMENTed ID fields (i.e. each table's primary key).  However, the fields in question are not set as AUTO_INCREMENT in the database, as they are not the primary key fields for these tables.  To the best of my knowledge, the only triggers defined on the entire database are the ones we have added to maintain a custom table, so these non-primary-key fields are not being set by a trigger.  I therefore had assumed that Moodle somehow manages these values - though it'd be great if that wasn't the case of course.

In reply to Martin Greenaway

Re: Duplicate entry when starting a quiz attempt

by Martin Greenaway -

OK so I've done more investigation and I have a further question.

Should the ID of the row in table mdl_question_usages correspond to the mdl_question_attempts.questionusageid and mdl_quiz_attempts.uniqueid?  Because, if so, this is where the disconnect is - there are fewer rows in that table than I'd expect with a max value of 2071 and that seems to correspond to where we started to see these issues.  I'd assumed they weren't related (despite naming convention) as they didn't appear to match up, but maybe that's the cause - something wrong with the restoration of that table perhaps?

In reply to Martin Greenaway

Re: Duplicate entry when starting a quiz attempt

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

"Should the ID of ..."

Yes. This is what is know as a foreign key relationship in database. And although Moodle does not acutally create the constrains in the database schema, they are defined in the files like lib/db/install.xml and mod/quiz/db/install.xml. You can see this by going to Admin -> Development -> XMLDB in your Moodle site, and clicking the [ Doc ] link.

If this only happens on copies of your production site, it seems natural to start by debugging the copy process. That is the think that is most likely to have casued the problem.

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Duplicate entry when starting a quiz attempt

by Martin Greenaway -

Thanks Tim, the lack of explicit FK constrains can have a number of reasons obviously, but isn't helpful when trying to understand the schema.  I'd forgotten about checking in XMLDB though - thank you for pointing this out!