Upgrading 4.01 to latest 4.1.2+: Duplicate Database Entry

Upgrading 4.01 to latest 4.1.2+: Duplicate Database Entry

by Lukas Müller -
Number of replies: 2

Hi dear Community,

I am facing Errors, when i try to update Moodle:
The Following Error occures:

Debug-Info:  Duplicate entry '7130-55959' for key 'mdl_course_modules_viewed.mdl_courmoduview_usecou_uix'
INSERT INTO mdl_course_modules_viewed
(userid, coursemoduleid, timecreated)
SELECT userid, coursemoduleid, timemodified
FROM mdl_course_modules_completion
WHERE viewed = 1
[array (
)]
Error code: dmlwriteexception

Stack trace: 

  • line 497 of /lib/dml/moodle_database.php: dml_write_exception thrown
  • line 293 of /lib/dml/moodle_read_slave_trait.php: call to moodle_database->query_end()
  • line 1172 of /lib/dml/mysqli_native_moodle_database.php: call to mysqli_native_moodle_database->query_end()
  • line 2980 of /lib/db/upgrade.php: call to mysqli_native_moodle_database->execute()
  • line 1891 of /lib/upgradelib.php: call to xmldb_main_upgrade()
  • line 527 of /admin/index.php: call to upgrade_core()


A week ago we had to restore the moodle instance (database / moodleweb and moodledata folder) because a important plugin (content pages) was deletet by accident.
I assume that the problem is related to this.

Does anyone have any idea how I could solve this problem?

Thank you very much in advance

Lukas


Average of ratings: -
In reply to Lukas Müller

Re: Upgrading 4.01 to latest 4.1.2+: Duplicate Database Entry

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

That table, mdl_course_modules_viewed, is new in Moodle 4.1 so should be empty if upgrading from Moodle 4.0. It looks like this error is because it already contains data, perhaps there was an earlier upgrade that was abandoned but not fully rolled back to the previous version's database.

You should be able to just delete the records from that table, for example with the SQL:

DELETE FROM mdl_course_modules_viewed;

As a precaution you should back up the database before making changes such as this.

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

Re: Upgrading 4.01 to latest 4.1.2+: Duplicate Database Entry

by Lukas Müller -
Awesome, this did the trick!
You saved my weekend, thank you very much!