Skip to content

update/upsert 可能会将不在 raw 参数里的字段从 undefined 设为 null #91

@chuan6

Description

@chuan6

实际发生的场景:

  • 受到影响的那个字段是 OBJECT 类型

  • raw 参数只包含主键和一个与受影响字段无关(不存在关联)的字段

  • 手动 upsert { 主键: 'xxx', 受影响字段: undefined },可以将该字段值重置为 undefined

  • 构建一个失败的单元测试以确认问题

在 test/schemas/Post 里,添加 attachments 字段,设置其类型为 RDBType.OBJECT,

it.only("should keep an `undefined` attribute unchanged if the attribute is absent from the upsert body", function*() {
  const mockPost = postGen(1, null).pop()
  const { _id, content, created } = mockPost

  // 需要先有这个条目才能复现原来 undefined 的属性被改为 null 的行为。
  yield database.upsert('Post', { _id, created })

  const post = { _id, content }
  const execRet = yield database.update('Post', post) // upsert 也能重现,insert 不会

  const [ret] = yield database
    .get<PostSchema>('Post', {
      where: {
        _id: post._id,
      },
    })
    .values()

  expect(ret).to.deep.equal({ _id, content, created })
  checkExecutorResult(execRet, 1)
})

得:

AssertionError: expected { Object (_id, content, ...) } to deeply equal { Object (_id, content, ...) }
+ expected - actual

{
    "_id": "47675ecd"
-  "attachments": null
    "content": "posts content:ab8920dd"
    "created": "1969-12-31T16:00:00.000Z"
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions