Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-69789

Column 'groupid' cannot be null

    XMLWordPrintable

    Details

    • Database:
      MySQL
    • Testing Instructions:
      Hide

      Create a webservice, connect to it and try to create a new wiki via the json REST api with the following parameters:

      {
       "moodlewsrestformat": "json",
       "wstoken": "mytoken",
       "wikiid": 2,
       "title": "Testpage",
       "content": "Hello World!",
       "wsfunction": "mod_wiki_new_page"
      }
      

      The corresponding API-Call: https://mymoodle/webservice/rest/server.php?moodlewsrestformat=json&wstoken=mytoken&wikiid=2&title=Testpage&content=Hello+World%21&wsfunction=mod_wiki_new_page

      You may need to adjust the wikiid to match the id of your wiki.

      Without this patch, it should fail with this error message:

      {
       "exception": "dml_write_exception",
       "errorcode": "dmlwriteexception",
       "message": "Fehler beim Schreiben der Datenbank",
       "debuginfo": "Column 'groupid' cannot be null\nINSERT INTO mdl_wiki_subwikis (wikiid,groupid,userid) VALUES(?,?,?)\n[array (\n 0 => '2',\n 1 => NULL,\n 2 => 0,\n)]"
      }
      

       

      With this patch applied, it should return something like this:

      code:json

      {"pageid": 5, "warnings": []}

      code

      The page ID may differ and the warnings object may not be present without debug mode enabled.

      Show
      Create a webservice, connect to it and try to create a new wiki via the json REST api with the following parameters: { "moodlewsrestformat": "json", "wstoken": "mytoken", "wikiid": 2, "title": "Testpage", "content": "Hello World!", "wsfunction": "mod_wiki_new_page" } The corresponding API-Call: https://mymoodle/webservice/rest/server.php?moodlewsrestformat=json&wstoken=mytoken&wikiid=2&title=Testpage&content=Hello+World%21&wsfunction=mod_wiki_new_page You may need to adjust the wikiid to match the id of your wiki. Without this patch, it should fail with this error message: { "exception": "dml_write_exception", "errorcode": "dmlwriteexception", "message": "Fehler beim Schreiben der Datenbank", "debuginfo": "Column 'groupid' cannot be null\nINSERT INTO mdl_wiki_subwikis (wikiid,groupid,userid) VALUES(?,?,?)\n[array (\n 0 => '2',\n 1 => NULL,\n 2 => 0,\n)]" }   With this patch applied, it should return something like this: code:json {"pageid": 5, "warnings": []} code The page ID may differ and the warnings object may not be present without debug mode enabled.
    • Workaround:
      Hide

      Update the default value for group ID to be zero instead of null.

      Show
      Update the default value for group ID to be zero instead of null.
    • Difficulty:
      Easy
    • Affected Branches:
      MOODLE_39_STABLE
    • Pull from Repository:
    • Pull 3.9 Branch:
    • Pull Master Branch:

      Description

      When creating a new wiki page using the web-service method mod_wiki_new_page with the following parameters:

      {
        "moodlewsrestformat": "json",
        "wstoken": "mytoken",
        "wikiid": 2,
        "title": "Testpage",
        "content": "Hello World!",
        "wsfunction": "mod_wiki_new_page"
      }
      

      The corresponding API-Call: https://mymoodle/webservice/rest/server.php?moodlewsrestformat=json&wstoken=mytoken&wikiid=2&title=Testpage&content=Hello+World%21&wsfunction=mod_wiki_new_page

      I get the error message dml_write_exception. The full exception with debug enabled looks like this:

      {
        "exception": "dml_write_exception",
        "errorcode": "dmlwriteexception",
        "message": "Fehler beim Schreiben der Datenbank",
        "debuginfo": "Column 'groupid' cannot be null\nINSERT INTO mdl_wiki_subwikis (wikiid,groupid,userid) VALUES(?,?,?)\n[array (\n  0 => '2',\n  1 => NULL,\n  2 => 0,\n)]"
      }
      

      This bug therefore appears to be with the default value for groupid. According to the code and the generated documentation, null is the default value used for creating new wiki pages. However, this doesn't fit with the NOTNULL=true as defined in install.xml.

      When taking a look at the database, previous wiki pages were created with the groupid 0. Therefore, I created the following fix:

      diff --git a/mod/wiki/classes/external.php b/mod/wiki/classes/external.php
      index 4a147559517..70ee6989ab1 100644
      --- a/mod/wiki/classes/external.php
      +++ b/mod/wiki/classes/external.php
      @@ -938,7 +938,7 @@ class mod_wiki_external extends external_api {
                       'userid' => new external_value(PARAM_INT, 'Subwiki\'s user ID. Used if subwiki does not exists.', VALUE_DEFAULT,
                           null),
                       'groupid' => new external_value(PARAM_INT, 'Subwiki\'s group ID. Used if subwiki does not exists.', VALUE_DEFAULT,
      -                    null)
      +                    0)
                   )
               );
           }
      @@ -957,7 +957,7 @@ class mod_wiki_external extends external_api {
            * @since Moodle 3.1
            */
           public static function new_page($title, $content, $contentformat = null, $subwikiid = null, $wikiid = null, $userid = null,
      -        $groupid = null) {
      +        $groupid = 0) {
               global $USER;
       
               $params = self::validate_parameters(self::new_page_parameters(),
      

      This patch replaces the default value for the GrouID with 0 and fixed the issue on my installation.

      My moodle installation is running on the following specs:

      • MOODLE_39_STABLE (a45e6838efe43d6a4485e911a7e81a88edc0f53e)
      • Debian Buster, 10.4
      • 4.19.0-9-amd64
      • MariaDB 10.3.22 database server

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            moritz.fromm Moritz 'e1mo' Fromm
            Participants:
            Component watchers:
            Amaia Anabitarte, Carlos Escobedo, Ferran Recio, Ilya Tregubov, Sara Arjona (@sarjona)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated: