Moodle | New Database table not getting created

| By Webner

In Moodle, we were creating a new table for our custom plugin in db/install.xml file.

Install.xml file :


<TABLE NAME="test_student" COMMENT="Student details.">


<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>

<FIELD NAME="user_name" TYPE="text" NOTNULL="true"  SEQUENCE="false"/>

<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>

<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="The time the user has been created."/>

<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="The most recent modification time for the user."/>



<KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="The unique id for this student."/>




Problem :
After saving this file the table does not get displayed in moodle database as well as on xmldb editor page. After upgrading the version number in version.php file nothing gets changed.
So tables are not created in moodle by just saving the file or upgrading the version number of plugin in version.php.

Solution :
As the file name implies (install.xml), it gets only executed at the time of plugin installation. To create a new table in already existing plugin in moodle, we need to uninstall it first and then install it again.
After installing it again we can see the new table in database and xmldb editor.
We need to do the same for any changes in table structure.

Leave a Reply

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