Closed
Description
example:
table:
CREATE TABLE t_order_item
(
item_id
bigint(20) NOT NULL,
order_id
bigint(20) unsigned DEFAULT NULL,
user_id
int(11) NOT NULL,
status
varchar(45) DEFAULT NULL,
creation_date
date DEFAULT NULL,
PRIMARY KEY (item_id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
config_sharding:
shardingRule:
tables:
t_order:
actualDataNodes: ds_${0..5}.t_order_${0..1}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
# keyGenerator:
# type: SNOWFLAKE
# column: order_id
t_order_item:
actualDataNodes: ds_${0..5}.t_order_item_${0..1}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_item_${order_id % 2}
keyGenerator:
type: SNOWFLAKE
column: item_id
case1:
INSERT INTO t_order_item(order_id,user_id,status,creation_date) VALUES (2111,21,' init',' 2017-08-31');
select @@IDENTITY as insert_id from t_order_item;
result is not correct!
case2:
INSERT INTO t_order_item(order_id,user_id,status,creation_date) VALUES (2111,21,' init',' 2017-08-31');
SELECT LAST_INSERT_ID();
result is not correct!
Activity
shihuizhen commentedon Oct 27, 2020
apache-shardingsphere-4.1.0-sharding-proxy-bin
tristaZero commentedon Oct 27, 2020
Hi @shihuizhen
What is the correct result you expected? And why did you think the result is incorrect?
Lucas-307 commentedon Oct 27, 2020
@shihuizhen HI, I don't think
select @@IDENTITY as insert_id from t_order_item;
is correct sql. Maybe you meanselect @@IDENTITY as insert_id;
Foucs on this Question.
As we know, there are several actual datasources.
INSERT INTO t_order_item(order_id,user_id,status,creation_date) VALUES (2111,21,' init',' 2017-08-31')
will insert into a specific datasource.but
select @@IDENTITY
orSELECT LAST_INSERT_ID()
will execute in a random datasource.(do not have sharding keys)So, we can't make sure
select @@IDENTITY
orSELECT LAST_INSERT_ID()
is what you want.Lucas-307 commentedon Oct 27, 2020
@shihuizhen Actually, I tested this two sql (
select @@IDENTITY
orSELECT LAST_INSERT_ID()
), the result not only depends on which datasource but also database did you use.shihuizhen commentedon Oct 27, 2020
@Lucas-307 Thanks.
Is there any way to get id of new row?
When connect MySQL directly, I can get the new row's PRIMARY KEY like this:
When use sharding-proxy how to?
Lucas-307 commentedon Oct 27, 2020
@shihuizhen Do you use jdbc?
shihuizhen commentedon Oct 27, 2020
@Lucas-307
result is -1
connect MySQL directly:
result is correct!
Lucas-307 commentedon Oct 28, 2020
@shihuizhen I can get id by this function in
master
version, I'm not sure if it is ok inv4
.Lucas-307 commentedon Oct 28, 2020
@tristaZero Can we get
lastInsertId
by jdbcps.getGeneratedKeys()
in version 4?like this:
tristaZero commentedon Oct 28, 2020
Hi @shihuizhen @Lucas-307
I did not look through all the conversation between you. If
PreparedStatement
comes fromShardingSphereConnection
,ps.getGeneratedKeys
can work well for sharding tables. However, I have to say there is a bug when get generated keys after a batch insert SQL, which has been fixed in the forthcoming 5.x release.Lucas-307 commentedon Oct 28, 2020
OK Thanks.
shihuizhen commentedon Oct 28, 2020
Which do you using for this test? mysql-jdbc or sharding-jdbc?
mysql-jdbc connect to sharding-proxy_v4.1.1 is not OK.
Lucas-307 commentedon Oct 29, 2020
sharding-jdbc master version.
Lucas-307 commentedon Oct 29, 2020
@shihuizhen As @tristaZero said, there is a bug in 4.x, which has been fixed in the forthcoming 5.x release.
kimmking commentedon Oct 30, 2020
@shihuizhen
Statement.RETURN_GENERATED_KEYS and using Key-generator in 4.x works well, but without key-gen doesn't.
you can wait for 5.0.0 version, this week release.
@@identity or last_insert_id has no plan to support now.
you can try to fix it.
terrymanu commentedon Dec 19, 2021
I just close the issue because of no response anymore.