关于 session.Insert 代码的一个疑问 #400

Closed
opened 2016-05-23 08:36:16 +00:00 by shenli · 5 comments
shenli commented 2016-05-23 08:36:16 +00:00 (Migrated from github.com)

@lunny 我看了一下Insert相关的代码,这里不太理解。
对于一个 AutoIncrement 的列,如果insert的时候,已经明确指定了值(这个时候应该是aiValue!=nil吧?),应该直接返回,否则需要通过LastInsertID把自动生成的id填到aiValue中,所以这里的if条件是不是写反了?

多谢!

@lunny 我看了一下Insert相关的代码,[这里](https://github.com/go-xorm/xorm/blob/master/session.go#L3153)不太理解。 对于一个 AutoIncrement 的列,如果insert的时候,已经明确指定了值(这个时候应该是aiValue!=nil吧?),应该直接返回,否则需要通过LastInsertID把自动生成的id填到aiValue中,所以这里的if条件是不是写反了? 多谢!
if aiValue == nil || !aiValue.IsValid() /*|| aiValue.Int() != 0*/ || !aiValue.CanSet() {

这行的判断是如果没有取到自增字段就不回填了。中间的注释是你说的如果已经有值就不回填,这个地方需要优化下,目前是始终回填。

``` Go if aiValue == nil || !aiValue.IsValid() /*|| aiValue.Int() != 0*/ || !aiValue.CanSet() { ``` 这行的判断是如果没有取到自增字段就不回填了。中间的注释是你说的如果已经有值就不回填,这个地方需要优化下,目前是始终回填。
shenli commented 2016-05-23 15:17:49 +00:00 (Migrated from github.com)

对于 MySQL 来说,如果明确指定了值,就不会更新lastinsertid,所以始终回填是否是有问题的?
mysql> create table t (c int auto_increment key);
Query OK, 0 rows affected (0.14 sec)

mysql> insert into t values ();
Query OK, 1 row affected (0.02 sec)

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 1 |
+------------------+
1 row in set (0.01 sec)

mysql> insert into t values (100);
Query OK, 1 row affected (0.01 sec)

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)

mysql>

对于 MySQL 来说,如果明确指定了值,就不会更新lastinsertid,所以始终回填是否是有问题的? mysql> create table t (c int auto_increment key); Query OK, 0 rows affected (0.14 sec) mysql> insert into t values (); Query OK, 1 row affected (0.02 sec) mysql> select last_insert_id(); +------------------+ | last_insert_id() | +------------------+ | 1 | +------------------+ 1 row in set (0.01 sec) mysql> insert into t values (100); Query OK, 1 row affected (0.01 sec) mysql> select last_insert_id(); +------------------+ | last_insert_id() | +------------------+ | 1 | +------------------+ 1 row in set (0.00 sec) mysql>

嗯。有这个可能,这应该是一个Bug。

嗯。有这个可能,这应该是一个Bug。

在前面解析last_insert_id的时候,如果为0,则不回填值。

在前面解析last_insert_id的时候,如果为0,则不回填值。
shenli commented 2016-06-30 09:05:51 +00:00 (Migrated from github.com)

好的 我再试试

好的 我再试试
lunny referenced this issue from a commit 2020-03-09 02:52:44 +00:00
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#400
No description provided.