Info
Content

Error writing to database field doesn't have a default value


If you receive an error like this, it indicates that your database schema may be missing some defaults:

Default exception handler: Error writing to database Debug: 
Field 'idnumber' doesn't have a default value

In this example, the error occurred when creating a new user account. To confirm, you can use the XMLDB editor to check your defaults:

Site administration > Development > XMLDB Editor > Check Defaults

This will allow you to run a check on the database and pick up any missing defaults. It will also generate the database SQL to fix this. For example, the check in this scenario found 4 defaults missing on the mdl_user table for the columns idnumber, phone1, phone2 and theme. It provided the following SQL to run on the database to fix:

ALTER TABLE mdl_user MODIFY COLUMN idnumber VARCHAR(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' after password;
ALTER TABLE mdl_user MODIFY COLUMN phone1 VARCHAR(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' after emailstop;
ALTER TABLE mdl_user MODIFY COLUMN phone2 VARCHAR(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' after phone1;
ALTER TABLE mdl_user MODIFY COLUMN theme VARCHAR(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' after calendartype;

Once you run the SQL on the database, you can use the same functionality to confirm that the defaults are now in place.

No Comments
Back to top