I found an insertForce method being called in Magento Core module. What is the difference of insertForce and insert method?

1 Answer 1


The function lies here vendor/magento/framework/DB/Adapter/Pdo/Mysql.php

public function insertForce($table, array $bind)
    $result = $this->insert($table, $bind);
    $this->rawQuery("SET SQL_MODE=IFNULL(@OLD_INSERT_SQL_MODE,'')");

    return $result;

So obviously insertForce set a few MySQL parameters before calling the normal insert function. The most important thing we are looking at is NO_AUTO_VALUE_ON_ZERO.

In short, if you relies on the auto increment column, inserting value 0 into that column with insert might not mean the inserted value will actually be 0 but rather the auto incremented value; while using insertForce will guarantee it to be 0.

I found this explanation is best (from Drupal community but it has nothing to do with Drupal)

NO_AUTO_VALUE_ON_ZERO affects handling of AUTO_INCREMENT columns. Normally, you generate the next sequence number for the column by inserting either NULL or 0 into it. NO_AUTO_VALUE_ON_ZERO suppresses this behavior for 0 so that only NULL generates the next sequence number.

This mode can be useful if 0 has been stored in a table's AUTO_INCREMENT column. (Storing 0 is not a recommended practice, by the way.) For example, if you dump the table with mysqldump and then reload it, MySQL normally generates new sequence numbers when it encounters the 0 values, resulting in a table with contents different from the one that was dumped. Enabling NO_AUTO_VALUE_ON_ZERO before reloading the dump file solves this problem. mysqldump now automatically includes in its output a statement that enables NO_AUTO_VALUE_ON_ZERO, to avoid this problem.

From: https://www.drupal.org/forum/deprecated/deprecated-database-compatibility/2007-08-03/set-sql_mode-no_auto_value_on_zero

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.