-
Notifications
You must be signed in to change notification settings - Fork 81
MySql Conditional Updates
Artiom Chilaru edited this page Jun 26, 2019
·
3 revisions
UPDATE: This is no longer the case, since a workaround has been implemented in #47
This page will be left here, since exceptions from the older version still reference it.
Due to MySQL database constraints, adding conditional filters to which rows would be updated during an upsert operation seems to be impossible.
For example, if we have this entry in our database:
new TestEntity
{
Num1 = 1,
Num2 = 7,
Text1 = "hello",
Text2 = "world",
}
And we try to upsert the following:
new TestEntity
{
Num1 = 1,
Num2 = 2,
Text1 = "who",
Text2 = "where",
}
We could try generate a query like this:
INSERT INTO `TestEntities` (`Num1`, `Num2`, `NumNullable1`, `Text1`, `Text2`, `Updated`)
VALUES (@p0, @p1, @p2, @p3, @p4, @p5)
ON DUPLICATE KEY UPDATE
`Num2` = IF (`Num2` != VALUES(`Num2`), VALUES(`Num2`), `Num2`),
`NumNullable1` = IF (`Num2` != VALUES(`Num2`), VALUES(`NumNullable1`), `NumNullable1`),
`Text1` = IF (`Num2` != VALUES(`Num2`), VALUES(`Text1`), `Text1`),
`Text2` = IF (`Num2` != VALUES(`Num2`), VALUES(`Text2`), `Text2`),
`Updated` = IF (`Num2` != VALUES(`Num2`), VALUES(`Updated`), `Updated`)
Problem is, it looks like MySql is updating these columns in separate operations. After updating the Num2
column - the other columns will not be updated, since by that point, the Num2
matches the value that we tried to insert.
As such, this functionality is not currently supported with MySql