XMLDB Editor File Not Installing Tables

XMLDB Editor File Not Installing Tables

by Steven Norris -
Number of replies: 5

I have a very simple plugin with the following structure:

  • lib.php
  • version.php
  • license.md
  • readme.md
  • db

I generated the basic structure using the "Moodle plugin skeleton generator" and then generated the install.xml file using the XMLDB Editor in core Moodle. From there, I uninstalled and removed all my files out of the enrol director to a different place (to make sure it was a "clean" install). Then moved the files back. Moodle picked up that they need to be installed and clicked "Upgrade Moodle Databse", but none of my tables are created. The docs are a little lacking in several places for Moodle, and I'm very new to this. My understanding was this install.xml file would automatically pick up and create the tables. What am I missing in order to get my tables to install?

install.xml


<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="enrol/open_course_code/db" VERSION="20200404" COMMENT="Open Course Code plugin tables" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd" >
<TABLES>
<TABLE NAME="occ_cohorts" COMMENT="Open Course Code plugin management of cohort code numbers">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="cohort_id" TYPE="int" LENGTH="19" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="allowed_codes" TYPE="int" LENGTH="20" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="occ_cohort_ids" TYPE="foreign-unique" FIELDS="cohort_id" REFTABLE="mdl_cohort" REFFIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="occ_codes" COMMENT="Open Course Code plugin codes">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="code" TYPE="char" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="used" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="revoked" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="occ_cohort_id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="user_id" TYPE="int" LENGTH="19" NOTNULL="false" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="occ_code_cohort" TYPE="foreign" FIELDS="occ_cohort_id" REFTABLE="occ_cohorts" REFFIELDS="id"/>
<KEY NAME="occ_user_code" TYPE="foreign" FIELDS="user_id" REFTABLE="mdl_user" REFFIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
Average of ratings: -
In reply to Steven Norris

Re: XMLDB Editor File Not Installing Tables

by Steven Norris -

Well... If feel dumb. It is working, but they prefix all tables even your custom ones, so the tables were not occ_codes, they were mdl_occ_codes. Soooo when I was querying the tables, they were not found because it prefixes the names of the tables, but also the documentation does not make this clear.

In reply to Steven Norris

Re: XMLDB Editor File Not Installing Tables

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
If it wasn't obvious to you it won't be obvious to others. You could add something to the Wiki here
https://docs.moodle.org/38/en/Plugin_skeleton_generator
To comment on your experience and help other users.
In reply to Marcus Green

Re: XMLDB Editor File Not Installing Tables

by Steven Norris -
It didn't have anything to do with the plugin. It was the XMLDB Editor, which takes a table name, but appends "mdl_" to it without really documenting that it does that.
In reply to Steven Norris

Re: XMLDB Editor File Not Installing Tables

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators

Just to clarify, the table names prefix is a standard Moodle core feature. The behavior is defined in the config.php (via the $CFG->prefix value).

Also note that in almost all cases (and especially if you plan to share your plugin), the table names should be named so that they start with the component name. E.g. if your plugin is local_occ then the tables should be defined with names like local_occ_cohorts etc.

In reply to David Mudrák

Re: XMLDB Editor File Not Installing Tables

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
And also that some databases including MySQL have limitations on the length of table names, so if your plugin is called a table with a name of local_cleverplugin_robotisbetterthanateacher
My cause issues.