Closed andrenam closed 6 years ago
Hi, thanks for the detailed reporting. I've made a commit to fix the id=0. If the id is null, it can be omitted in order to use the other unique field. And about not ovewriding the optional_column, currently is not possible. We could customize this, for example, accepting a callback to filter the columns you want to replace. Something like:
$query->duplications(function ($data) {
return array_filter($data); //Update only non-empty fields
});
My only concern is how to do this in sqlite. I'd like to keep the same behaviour in mysql/mariadb and sqlite.
Thanks, problem solved.
I saw that my problem with overwriting the optional_column doesnt occur, when I use the syntax
$db->test->insert()->data(array('unique_column' => 5))->run();
instead of
$new_test = $db->test->create();
$new_test->unique_column = 5;
// leave out optional column
$new_test->save();
I'm having problems with the duplications() modifier for Insert statements. I have a table with auto-incrementing ID and a unique column.
database used
When I try to insert a (duplicate) row without specififying the (autoincrementing) ID and giving a duplicate value for the unique column, the INSERT .. ON DUPLICATE KEY UPDATE statement overwrites the primary key with id=0.
script used
run the script
Expected and actual state of database after this:
The script continues to try to insert the second row:
Expected state of database after this:
Actual state of database after this:
Notice that the auto-incrementing id gets overwritten with 0 which leads to errors like this:
Software used:
reason:
The reason is that ID is specified twice in the ON DUPLICATE KEY UPDATE .. clause
... ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id),
id= :id, ...
quick n dirty hotfix?:
but what if the primary key is not called "id"? etc. etc. so this is not meant as a general bugfix but instead to help point you in the right direction.
Also what I would like to know, is it possible to not overwrite the non-specified field "optional_column" on the ON DUPLICATE KEY UPDATE?
So that the value in optional columns dont get overwritten when trying to insert a duplicate line. Maybe I should use an INSERT IGNORE ... statement instead then? Do you have any suggestions on how to do that?