mariadbmoodle

MariaDB, Moodle, Migration: Field 'id' doesn't have a default value


I'm migrating Moodle from shared hosting to a Server and ran into issues.

I have: Moodle v4.1.4, PHP 8.1 MariaDB 15.1, Debian, Froxlor

After moving all the files and database and updating the config.php I get the following error:

Error writing to database

Debug info: Field 'id' doesn't have a default value INSERT INTO mdl_sessions (state,sid,sessdata,userid,timemodified,timecreated,lastip,firstip) VALUES(?,?,?,?,?,?,?,?) [array ( 0 => 0, 1 => 'om9er1b27to3rqt5broqcknooi', 2 => NULL, 3 => 0, 4 => 1709207762, 5 => 1709207762, 6 => '95.90.242.199', 7 => '95.90.242.199', )] Error code: dmlwriteexception Stack trace:

line 497 of /lib/dml/moodle_database.php: dml_write_exception thrown line 293 of /lib/dml/moodle_read_slave_trait.php: call to moodle_database->query_end() line 1362 of /lib/dml/mysqli_native_moodle_database.php: call to mysqli_native_moodle_database->query_end() line 1408 of /lib/dml/mysqli_native_moodle_database.php: call to mysqli_native_moodle_database->insert_record_raw() line 573 of /lib/classes/session/manager.php: call to mysqli_native_moodle_database->insert_record() line 550 of /lib/classes/session/manager.php: call to core\session\manager::add_session_record() line 165 of /lib/classes/session/manager.php: call to core\session\manager::initialise_user_session() line 139 of /lib/classes/session/manager.php: call to core\session\manager::start_session() line 842 of /lib/setup.php: call to core\session\manager::start() line 33 of /config.php: call to require_once() line 30 of /index.php: call to require_once()

I really don't know where to start and how to fix it and long searches didn't help me. Any help is highly appreciated!


Solution

  • When you install Moodle, the 'id' field of every table created is set as the primary key and is set to "auto increment".

    This means that you should not be specifying the value for this 'id' field, it should be automatically generated (generally it will be "the last number used + 1").

    If that is not happening, then it suggests something has gone wrong with the table definitions whilst transferring the data.

    You may be able to get more information about this by logging in to your server and running the following script:

    php /path/to/moodle/admin/cli/check_database_schema.php
    

    (where "/path/to/moodle" is the actual path to where the Moodle code is found on the server)

    This will output a list of all the places where the database structure does not match the expected structure.

    If it is only one or two places, then you might be able to manually fix using a database editing tool (adminer, phpmyadmin or the mysql command-line tool). If it is more extensive than that, you might need to review the process you used to transfer the database and start that over again.