Moodle

setAdvanced function tried to insert duplicate key record into user prefs table

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Not a bug
  • Affects Version/s: 1.9.5
  • Fix Version/s: None
  • Component/s: Forms Library
  • Labels:
    None
  • Environment:
    Windows XP / ubuntu
  • Database:
    MySQL
  • Difficulty:
    Moderate
  • Affected Branches:
    MOODLE_19_STABLE

Description

if you use the Forms library function
$mform->setAdvanced(formControlName);
to toggle the visibility of form controls, and then set the form action to be the same page (containing the form), it is possible for an attempt to be made to insert a duplicate key into user_preferences:

eg
INSERT INTO mdl2_user_preferences ( USERID, NAME, VALUE ) VALUES ( 66, 'mod_rclearningresource_mod_form_search_showadvanced', '0' )

where userid and name are a key of type unique

  1. file1.txt
    20/Jun/09 6:47 PM
    1 kB
    dave brad
  2. form_definition.php
    20/Jun/09 6:47 PM
    3 kB
    dave brad

Activity

Hide
Jamie Pratt added a comment -

Hi,

Could you provide an example of code for a form where you see this problem?

Jamie

Show
Jamie Pratt added a comment - Hi, Could you provide an example of code for a form where you see this problem? Jamie
Hide
dave brad added a comment -

The function in file1.txt is used on a standard moodle page (in our case not in a mod or block) to create the form, and display the form again after submission (the results of the search would appear below; I've commented out this code as it's not relevant).

The form definition is in form_definition.php. the form will give errors as some of the controls use data from our tables; I don't think this matters, or comment out form controls as needed.

To see the bug, display the form, switch the showAdvanced status, submit the form. Do this a couple of times, so records for both preferences are put into user-prefs table. Then you get warning
Duplicate entry '66-mod_rclearningresource_mod_form_search_showadvance' for key 2

INSERT INTO mdl2_user_preferences ( USERID, NAME, VALUE ) VALUES ( 66, 'mod_rclearningresource_mod_form_search_showadvanced', '1' )

Show
dave brad added a comment - The function in file1.txt is used on a standard moodle page (in our case not in a mod or block) to create the form, and display the form again after submission (the results of the search would appear below; I've commented out this code as it's not relevant). The form definition is in form_definition.php. the form will give errors as some of the controls use data from our tables; I don't think this matters, or comment out form controls as needed. To see the bug, display the form, switch the showAdvanced status, submit the form. Do this a couple of times, so records for both preferences are put into user-prefs table. Then you get warning Duplicate entry '66-mod_rclearningresource_mod_form_search_showadvance' for key 2 INSERT INTO mdl2_user_preferences ( USERID, NAME, VALUE ) VALUES ( 66, 'mod_rclearningresource_mod_form_search_showadvanced', '1' )
Hide
Jamie Pratt added a comment - - edited

Hi,

Aren't you getting any errors from your db? In my postgresql db I get an error as the name field value is too long to insert into the user_preferences record.

It seems your class name is too long. The name field in the preferences table is limited to just 50 characters in my db. Which would mean that form class names should be limited to 37 characters if you want to use $mform->setAdvanced

Jamie

Show
Jamie Pratt added a comment - - edited Hi, Aren't you getting any errors from your db? In my postgresql db I get an error as the name field value is too long to insert into the user_preferences record. It seems your class name is too long. The name field in the preferences table is limited to just 50 characters in my db. Which would mean that form class names should be limited to 37 characters if you want to use $mform->setAdvanced Jamie
Hide
dave brad added a comment -

You are absolutely right. The name field in the preferences table is limited to 50 characters in my db (mysql) is limited to 50 characters too. Once I renamed the form class name the error disappeared.

The error message I was getting was misleading: I suppose it tries an insert because it looks to see if the record it wants to insert is already there, finds it isn't (because the field value has been truncated) and so thinks an INSERT is what's needed rather than an UPDATE.

Unfortunately mysql doesn't throw an error when a record with a field value which is too long is inserted: it just truncates it. Perhaps postgresql is better in this regard.

Anyway thanks again: is it up to me to close this bug now?

Show
dave brad added a comment - You are absolutely right. The name field in the preferences table is limited to 50 characters in my db (mysql) is limited to 50 characters too. Once I renamed the form class name the error disappeared. The error message I was getting was misleading: I suppose it tries an insert because it looks to see if the record it wants to insert is already there, finds it isn't (because the field value has been truncated) and so thinks an INSERT is what's needed rather than an UPDATE. Unfortunately mysql doesn't throw an error when a record with a field value which is too long is inserted: it just truncates it. Perhaps postgresql is better in this regard. Anyway thanks again: is it up to me to close this bug now?

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: