ON DUPLICATE KEY UPDATE support incompletely on mybatis
Bug Report
For English only, other languages will not accept.
Before report a bug, make sure you have:
- Searched open and closed GitHub issues.
- Read documentation: ShardingSphere Doc.
Please pay attention on issues you submitted, because we maybe need more details. If no response more than 7 days and we cannot reproduce it on current information, we will close it.
Please answer these questions before submitting your issue. Thanks!
Which version of ShardingSphere did you use?
4.0.0-RC2
Which project did you use? Sharding-JDBC or Sharding-Proxy?
Sharding-JDBC
Expected behavior
INSERT INTO table_a (id,col_1,col_2) VALUES(1,#{bean.val1}, #{bean.val2}) ON DUPLICATE KEY UPDATE col_2 = col_2 + #{bean.val2} should execute successfully.
Actual behavior
got exception
org.springframework.jdbc.BadSqlGrammarException:
### Error updating database. Cause: java.sql.SQLException: No value specified for parameter 3
### The error may involve com.duoku.vipintegral.dao.vipscore.UserScoreLevelMapper.insertOrUpdate-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO test_a (id,col_1,col_2) VALUES(1,?, ?) ON DUPLICATE KEY UPDATE col_2 = col_2 + ?
### Cause: java.sql.SQLException: No value specified for parameter 3
; bad SQL grammar []; nested exception is java.sql.SQLException: No value specified for parameter 3
Reason analyze (If you can)
Debug from the source,found that the num of parameters just only get from InsertValuesSegment, but the parameter after ON DUPLICATE KEY UPDATE is not included in the calculation.And I also can not find values that belong to 'OnDuplicateKey' expression in OnDuplicateKeyColumnsSegment.
https://github.com/apache/incubator-shardingsphere/blob/85f2ff877a7aaf122998964a0d03f7c9b2830e36/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/sharding/engnie/dml/ShardingInsertOptimizeEngine.java#L70
currentParameters do not contains the parameter for 'ON DUPLICATE KEY UPDATE',and parameters is full.
Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.
- sql
CREATE TABLE `user` (
`id` int(10) unsigned NOT NULL COMMENT '主键',
`col_1` varchar(20) NOT NULL DEFAULT '' COMMENT 'val_1',
`col_2` int(10) NOT NULL DEFAULT '0' COMMENT 'val_int_2',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO table_a (id,col_1,col_2) VALUES(1,#{bean.val1}, #{bean.val2}) ON DUPLICATE KEY UPDATE col_2 = col_2 + #{bean.val2}
- in mybatis3
- any shareding rule.
Example codes for reproduce this issue (such as a github link).
@zhegexiaohuozi Please use following format.
ON DUPLICATE KEY UPDATE col_2 = col_2 + VALUES(col_2)
INSERT INTO table_a (id,col_1,col_2) VALUES(1,#{bean.val1}, #{bean.val2}) ON DUPLICATE KEY UPDATE col_2 = col_2 + VALUES(col_2)
Fixed at #3237
I use the core of dev branch, The problem is still exist.
I still have this problem with 4.0.0-RC3 version when use VALUES like this: SQL: insert into t_xxxx(data_type, delivery_org_id,receive_org_id,actual_fee,actual_instead_goods_fee, create_time) values ( ?,?,?,?,?,?) ON DUPLICATE KEY UPDATE actual_fee = actual_fee + VALUES(?), actual_instead_goods_fee = actual_instead_goods_fee + VALUES(?),create_time = VALUES(?)
Cause: java.sql.SQLException: No value specified for parameter 7
does anyone solve this problem ?
actual_fee = actual_fee + VALUES(?) change to actual_fee = actual_fee + VALUES(actual_fee)
在 2019年12月19日,16:34,zhaoyongsheng1990 [email protected] 写道:
VALUES(?)
@lonyee1989 :
I need add dynamic parameter to actual_fee
in mapper file like this:
dynamic parameter => actual_fee = actual_fee + VALUES(actual_fee)
eg.. db — actual_fee = 2
parameter — 5
exec:
ON DUPLICATE KEY UPDATE actual_fee = actual_fee + VALUES(actual_fee)
then
db — actual_fee = 7
please try it!
在 2019年12月23日,09:20,zhaoyongsheng1990 [email protected] 写道:
VALUES(#{actualFee})
dynamic parameter => actual_fee = actual_fee + VALUES(actual_fee) eg.. db — actual_fee = 2 parameter — 5 exec: ON DUPLICATE KEY UPDATE actual_fee = actual_fee + VALUES(actual_fee) then db — actual_fee = 7 please try it! … 在 2019年12月23日,09:20,zhaoyongsheng1990 @.***> 写道: VALUES(#{actualFee})
I change mapper file like this: actual_fee = actual_fee + VALUES(actual_fee), it worked , thanks very much.
If insert value is different from update value, such as:
INSERT INTO table_a (id,col_1,col_2) VALUES(1,#{bean.val1}, #{bean.val2}) ON DUPLICATE KEY UPDATE col_2 = col_2 + #{bean.val5}
Is the syntax already supported? @lonyee1989 @terrymanu
I don't know what is it. We only can talk about SQL, not ORM frameworks.
Yeah,this issue should really be described as a support integrity issue for mybatis.Under mybatis, you do lose the variable after 'ON DUPLICATE KEY UPDATE'. Just watching the discussion, by the way.
What is your log of sql.show?
Closed for no response anymore