无法通过续集进行递增:关系“用户”的列“ 0”不存在”

时间:2019-05-06 06:58:47

标签: node.js postgresql express sequelize.js increment

我使用Sequelize 4.42.0和PostgreSQL 11.2并为使increment正常工作付出了很多努力,我按照documentation中的描述进行操作。

我到处搜索,但找不到解决方案。 我也尝试过:

await Users.update({ testIncrement: database.literal('testIncrement + 1') }, {
      where: { id: user.id }
})

代码

控制器

export const test = async ({ body, user }, res) => {
  try {
    await Users.increment('testIncrement', { where: { id: user.id }})
  } catch(error) {
    console.log(error)
  }
}

型号

const Users = database.define('users',
  {
    id: {
      primaryKey: true,
      type: Sequelize.UUID,
      defaultValue: Sequelize.UUIDV4
    },
    testIncrement: {
      defaultValue: 0,
      type: Sequelize.INTEGER,
    },
  }
)

export default Users

预期

testIncrement增加1

结果

error: column "0" of relation "users" does not exist
       at Connection.parseE (/Users/.../node_modules/pg/lib/connection.js:555:11)
       at Connection.parseMessage (/Users/.../node_modules/pg/lib/connection.js:380:19)
       at Socket.<anonymous> (/Users/.../node_modules/pg/lib/connection.js:120:22)
       at Socket.emit (events.js:189:13)
       at Socket.EventEmitter.emit (domain.js:441:20)
       at addChunk (_stream_readable.js:284:12)
       at readableAddChunk (_stream_readable.js:265:11)
       at Socket.Readable.push (_stream_readable.js:220:10)
       at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
     name: 'error',
     length: 121,
     severity: 'ERROR',
     code: '42703',
     detail: undefined,
     hint: undefined,
     position: '35',
     internalPosition: undefined,
     internalQuery: undefined,
     where: undefined,
     schema: undefined,
     table: undefined,
     column: undefined,
     dataType: undefined,
     constraint: undefined,
     file: 'analyze.c',
     line: '2346',
     routine: 'transformUpdateTargetList',
     sql:
      'UPDATE "users" SET "testIncrement"="testIncrement"+ 1,"0"=\'id\',"1"=\'testIncrement\',"2"=\'createdAt\',"3"=\'updatedAt\' WHERE "id" = \'adbe346a-b117-4f21-aa74-8a46d1ededae\' RETURNING *' }

2 个答案:

答案 0 :(得分:0)

请尝试使用此代码片段,它应该可以工作。

Users.find({where: { id: user.id }})
    .then((user) => {
      if(user == null) console.log("Invalid user")
       return User.update({
            testIncrement: user.testIncrement + 1
          })
    })
    .then((updated) => {
        if(updated == false) console.log("Cannot be updated")
        return updated;
    })

答案 1 :(得分:0)

好的,好新,我发现是什么代码造成了这个问题,坏的新问题,似乎是sequelize中的一个错误:

https://github.com/sequelize/sequelize/issues/8612

TL; DR-在我的用户模型中删除此问题已解决

df1 <- structure(list(v1 = c("M", "Z", "P", "Z"), v2 = c("Z", "Z", "Z", 
 "P"), v3 = c("P", "P", "M", "Z")), class = "data.frame",
  row.names = c(NA, -4L))