cyjake / leoric

👑 JavaScript ORM for MySQL, PostgreSQL, and SQLite.

Home Page:https://leoric.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

wmui opened this issue · comments

commented

当使用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方法返回的是一个索引值

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

commented

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

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

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

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

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

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

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

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

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

#398