【问题标题】:Insert in sequelize.query and then return the inserted row在 sequelize.query 中插入,然后返回插入的行
【发布时间】:2020-11-06 12:47:23
【问题描述】:

有没有办法通过 sequelize 中的原始插入查询返回插入的行?

returning 选项不会更改任何返回值。正在返回插入行的计数和一个空数组而不是插入的行:[ [], 1 ]

let contato = await sequelize.query(
    'INSERT INTO public.contato(nome, telefone, id_empresa, id_status) VALUES ($nome, $telefone, $id_empresa, $id_status);',
    {
        bind: {
            nome: form.nome,
            telefone: form.telefone,
            id_empresa: filaChat.id_empresa,
            id_status: 1,
        },
        type: QueryTypes.INSERT,
        returning: true,
    }
);

【问题讨论】:

  • 你在连接上为方言设置了什么?
  • dialect: 'postgres'

标签: node.js postgresql sequelize.js


【解决方案1】:

对于 postgress,您需要在原始查询中包含 RETURNING id

INSERT INTO public.contato(nome, telefone, id_empresa, id_status)
VALUES ($nome, $telefone, $id_empresa, $id_status)
RETURNING id;

【讨论】:

  • 感谢您迟到的答复。它就像一个魅力,让我免于执行其他查询来获取 ID。 returning: true 在这种情况下是没有用的。
猜你喜欢
  • 1970-01-01
  • 2018-01-04
  • 2015-02-23
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
相关资源
最近更新 更多