Upgrade from 1.9 to 2.0 error writing to database

Re: Upgrade from 1.9 to 2.0 error writing to database

by Chris Fryer -
Number of replies: 0

I experienced this error today.  It seems to be caused by having more than one record in the mdl_course_modules table referencing the same resource.

The error message will tell you the id of the resource that is referenced more than once.  Use the following SQL to locate it in your course:

SELECT r.id, r.course, r.name, r.type, r.reference, cm.id as cmid
FROM mdl_resource r
LEFT JOIN mdl_course_modules cm
ON (r.id = cm.instance)
WHERE r.id = <resource_id>;

Substitute the "duplicate entry" id number for <resource_id> above.  In the OP's case, this is '2029'.

Run the SQL in your database client.  You should get a result set like the following:

+-------+--------+------------------------+------+------------------------+-------+
| id    | course | name                   | type | reference              | cmid  |
+-------+--------+------------------------+------+------------------------+-------+
| 39090 |    835 | Exercise Suggestions 6 | file | estex06suggestions.htm | 50100 |
| 39090 |    835 | Exercise Suggestions 6 | file | estex06suggestions.htm | 50122 |
+-------+--------+------------------------+------+------------------------+-------+
2 rows in set (0.00 sec)

Inspect the list of resources in that course by crafting a URL from the "course" field, e.g. http://yourmoodle.ac.uk/mod/resource/index.php?id=835

Search in the page for the text in the "name" column, in my case "Exercise Suggestions 6".  The URL of the resource's link will tell you which cmid is in use, e.g. https://yourmoodle.ac.uk/mod/resource/view.php?id=50122

As long as there is only one link to the resource in the resource/index.php page, the other coursemodule record is safe to delete. So in my case, the following SQL deleted the record:

delete from mdl_course_modules where id = 50100;
Query OK (1 row affected );

Proceed with the upgrade as before.  It will pick up where you left off.