Moodle/Totara | Moodle not inserting values to new column added to an existing table

|
| By Webner

In Moodle, if we alter a table by adding a new column to it and use Moodle $DB->insert_record(‘table_name’,$dataobject) , it inserts the value which are assigned to old columns but not to the newly created columns.

For Example: if we have “queue” table having “cat”, “priminst”, “secinst” columns in it. Later on we alter the table, add a new column to it say “course” and then try to insert values to these columns as following:

$record1->cat = $category->id;
$record1->priminst = urlencode($instructorMoodleID);
$record1->secinst= urlencode($instructorRoleID);
$record1->course = urlencode($instructorAssistMoodleID);  //for new column
$ecrmqueueId = $DB->insert_record(‘queue', $record1);

The above code inserts values in “cat”, “priminst”, “secinst” columns, but not in the “course” column.

Solution: This is because insert_record() is calling get_columns() to retrieve the columns of the selected table. This function uses cached data instead of checking the table itself every time, so the changes we made aren’t included.
To solve this, we need to purge all caches for making this to work. We can purge caches by navigating to:

Site administration -> Development -> Purge all caches

Then try above code and it will work.

Leave a Reply

Your email address will not be published. Required fields are marked *