Amrita Deb
Amrita Deb

Reputation: 335

custom table doesnt exist in Moodle

I am building a block moodle plugin. For the plugin I have created three tables: 'block_learning_strategizer':contains just one field-id 'ls_basic': containing 3 fields-id,lp_name,description 'ls_path_details': containing 9 fields.

The definition is done through install.xml(under blocks/learning_strategizer/db) The XML is as below:

<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="blocks/learning_strategizer/db" VERSION="20120122" COMMENT="XMLDB file for Moodle blocks/learning_strategizer"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
    <TABLES>
        <TABLE NAME="block_learning_strategizer" COMMENT="Default for block_learning_strategizer" NEXT="ls_basic">
            <FIELDS>
                <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
            </FIELDS>
            <KEYS>
                <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
            </KEYS>
        </TABLE>

        <TABLE NAME="ls_basic" COMMENT="Table contains name and description of learning paths" NEXT="ls_path_details">
            <FIELDS>
                <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
                <FIELD NAME="lp_name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="id" NEXT="description"/>
                <FIELD NAME="description" TYPE="text" NOTNULL="false" SEQUENCE="false" PREVIOUS="lp_name"/>
            </FIELDS>
            <KEYS>
                <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
            </KEYS>
        </TABLE>

        <TABLE NAME="ls_path_details" COMMENT="Table contains details of created learning paths" PREVIOUS="ls_basic">
            <FIELDS>
                <FIELD NAME="id" SEQUENCE="true" TYPE="int"  NOTNULL="true" LENGTH="10" NEXT="lp_id"/>
                <FIELD NAME="lp_id" SEQUENCE="false" TYPE="int" LENGTH="10" NOTNULL="true" NEXT="id" PREVIOUS="course" />
                <FIELD NAME="course" SEQUENCE="false" TYPE="int" LENGTH="10" NOTNULL="true" NEXT="section" PREVIOUS="lp_id"/>
                <FIELD NAME="section" SEQUENCE="false" TYPE="int" NOTNULL="true" LENGTH="10" NEXT="req" PREVIOUS="course"/>
                <FIELD NAME="req" SEQUENCE="false" TYPE="int" NOTNULL="true" LENGTH="2" NEXT="inc" PREVIOUS="section"/>
                <FIELD NAME="inc" SEQUENCE="false" TYPE="int" NOTNULL="true" LENGTH="2" NEXT="modid" PREVIOUS="req"/>
                <FIELD NAME="modid" SEQUENCE="false" TYPE="int" NOTNULL="true" LENGTH="3" NEXT="seqno" PREVIOUS="inc"/>
                <FIELD NAME="seqno" SEQUENCE="false" TYPE="int" NOTNULL="true" LENGTH="10" NEXT="filename" PREVIOUS="modid"/>
                <FIELD NAME="filename" SEQUENCE="false" TYPE="text" NOTNULL="true" LENGTH="255" PREVIOUS="seqno"/>
            </FIELDS>
            <KEYS>
                <KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="lp_id"/>
                <KEY NAME="lp_id" TYPE="foreign" FIELDS="lp_id" REFTABLE="ls_basic" REFFIELDS="id" PREVIOUS="primary" />
            </KEYS>
        </TABLE>
    </TABLES>
</XMLDB>

However when I am trying to insert records I am getting an error :"Table "ls_basic" does not exist" I checked XMLDB editor from Site administration and I could see the tables have been made.

enter image description here

I havent included an upgrade.php but as far as I know that file is optional. It would be really helpful if someone could point out why do i get this error?

Upvotes: 1

Views: 1010

Answers (1)

davosmith
davosmith

Reputation: 6317

When a plugin is first installed, Moodle parses the install.xml file and uses this to create the database tables required for your plugin.

After the first installation of the plugin, Moodle does not look at the install.xml file again. Instead it relies on checking the upgrade.php file, at the point where your plugin's version number (in version.php) changes, in order to find out how to transform the previous database structure to match the new structure.

If your plugin is still under local development, you can get Moodle to re-parse the install.xml file by using the 'uninstall' feature in the 'Plugins' part of the 'Site administration' area. This will remove all the data for the plugin, then, if the code for the plugin still exists on the server, will immediately offer to re-install the plugin (which will create all the tables in install.xml).

If your plugin is already in use, or you do not want to lose any existing data, then you will need to use the XMLDB editor to generate the relevant lines of code to go in your upgrade.php file (and increase your plugin's version number to match).

See https://docs.moodle.org/dev/Upgrade_API for more details.

I would also suggest that this is a good time to fix your database tables to match the Moodle coding guidelines:

  • Variable names should not have _ characters in them - this applies to database field names as well (although _ in database table names are fine).
  • Plugin database tables should all start with the name of the plugin ('block_learning_stategizer' in this case) - if you end up using Travis CI to automatically check your plugins, then it will complain about your database table names.
  • Plugin names are strongly discouraged from having _ in them (other than between the plugin type and the rest of the name) - there have been a number of bugs over the years caused by Moodle core getting stuck on names that break this rule. It may be a good idea to rename your plugin 'block_learningstrategizer' now, before you hit any problems.

Upvotes: 1

Related Questions