I created a database table using this line:
CREATE TABLE mytable(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
uniquename VARCHAR(256) NOT NULL UNIQUE KEY, creationtime TIMESTAMP,
updatedtime TIMESTAMP);
Here is my dapper-dot-net INSERT OR UPDATE command:
const string uniquename = "25975B8F882E7B1DD99116B71C5A8D04";
// Has format "yyyy-MM-dd HH:mm:ss"
string mysqlTimeStampString = DateTime.UtcNow.ToMysqlTimeStampString();
dbConn.Execute(@"INSERT INTO mytable (uniquename, creationtime,
updatedtime) VALUES (@uniquename, @creationtime, @updatedtime) ON DUPLICATE
KEY UPDATE updatedtime=@updatedtime;", new { uniquename=uniquename,
creationtime = mysqlTimeStampString, updatedtime = mysqlTimeStampString });
After the first time I ran it, I select * from mytable \G and I got this result:
id: 1
uniquename: 25975B8F882E7B1DD99116B71C5A8D04
creationtime: 2016-01-25 00:06:55
updatedtime: 2016-01-25 00:06:55
So far everything looks good, but when I run the same INSERT OR UPDATE a few minutes later, I get this result:
id: 1
uniquename: 25975B8F882E7B1DD99116B71C5A8D04
creationtime: 2016-01-24 19:10:00
updatedtime: 2016-01-25 00:10:00
This is puzzling for the following reasons:
- On duplicate, only the
updatedtimefield is supposed to be updated, but both thecreationtimeandduplicatetimefields are being updated. Why are both fields being updated? - The
mysqlTimeStampStringstring is passed into the INSERT OR UPDATE command twice. Literally one string passed in twice, so there is absolutely no possibility thatcreationtimeis local time whileupdatedtimeis UTC. How on earth is it possible, during the duplicate update, that thecreationtimeis being converted to local time (UTC -5:00) while theupdatedtimeis set to UTC?
The best I can figure, this must be a dapper bug, mysql bug, or both, or my syntax is wrong.
ON DUPLICATE KEY UPDATE updatedtime = VALUES(updatedtime)from_unixtimewith TZ diff.