Questions on getGeneratedKeys with on duplicate key update cause
Posted by:
y kong
Date: March 16, 2022 09:40AM
Hi there, I was using mysql-connector-java:8.0.28 and trying to use getGeneratedKeys to get the inserted primary keys. The insert SQL is like: INSERT IGNORE INTO channel_info(platform, app_id, area_id, channel_name) VALUES (100, 100, 1, 'test-channel-1'), (100, 100, 2, 'test-channel-1') ON DUPLICATE KEY UPDATE `channel_name`=VALUES(`channel_name`);
There are two rows inserted into the table successfully, but getGeneratedKeys only returns the first row's primary key.
And I tried to debug into com.mysql.cj.jdbc.StatementImpl#getGeneratedKeys and found that when lastQueryIsOnDupKeyUpdate, 1 would be used as getGeneratedKeysInternal's parameter.
I was wondering why numsKey has to be 1 rather than getLargeUpdateCount()? Since when I manually execute getGeneratedKeysInternal() which using getLargeUpdateCount() as numsKey, it seems to returns all the correct primary keys' values.