Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug]插入数据成功后,返回的数据id和数据库不一致 #395

Closed
wmui opened this issue Jun 16, 2023 · 6 comments
Closed

[bug]插入数据成功后,返回的数据id和数据库不一致 #395

wmui opened this issue Jun 16, 2023 · 6 comments
Assignees

Comments

@wmui
Copy link

wmui commented Jun 16, 2023

当使用create方法插入一条数据:

POST http://127.0.0.1:7001/admin/role
Content-Type: application/json

{
  "id": "e32639c7-9573-4f90-828b-be410d58f272",
  "name": "test ",
  "authority": "100100, 100101"
}

返回的数据

HTTP/1.1 200 OK
content-type: application/json; charset=utf-8
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
x-download-options: noopen
x-readtime: 38
content-length: 156
Date: Fri, 16 Jun 2023 02:43:22 GMT
Connection: close

{
  "success": true,
  "data": {
    "id": "6",
    "name": "test ",
    "authority": "100100, 100101",
    "updatedAt": "2023-06-16T02:43:22.633Z",
    "createdAt": "2023-06-16T02:43:22.633Z"
  }
}

数据库里面的id是e32639c7-9573-4f90-828b-be410d58f272,而create方法返回的是一个索引值

@cyjake
Copy link
Owner

cyjake commented Jun 16, 2023

什么数据库?id 字段类型是什么

@wmui
Copy link
Author

wmui commented Jun 16, 2023

@cyjake SQLite,字段是STRING类型,通过UUID生成:

id: {
        type: STRING,
        primaryKey: true,
      }

@wmui
Copy link
Author

wmui commented Jun 16, 2023

还有一个问题,update执行后返回的是一个number,但是文档写的是spell实例:
image

实际update成功后返回了数字1

@cyjake
Copy link
Owner

cyjake commented Jun 16, 2023

知道了,sqlite 数据库有个自带的自增 id,leoric 执行完 insert 默认会用返回的自增 id 去设置对应的 id 字段,一般如果表结构里面 id 类型符合 biginit unsigned,会默认对应到这个自增 id 去

但是在你这个例子里面,这个 id 是不对应的,所以搞错了,我想想怎么处理

@cyjake cyjake self-assigned this Jun 16, 2023
@cyjake
Copy link
Owner

cyjake commented Jun 16, 2023

update执行后返回的是一个number,但是文档写的是spell实例:返回了数字1

这个其实没错的,update() 本身返回的是 Spell 实例,等同于 Promise,await 之后拿到的是 number,也就是 affectedRows

@cyjake
Copy link
Owner

cyjake commented Nov 7, 2023

@wmui 为了更好地兼容这种非自增 id 的使用场景, 我们去掉了使用 last insert id 覆盖模型主键的逻辑,你反馈的这个问题也一并修复了,抱歉隔了这么长时间

#398

@cyjake cyjake closed this as completed Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants