André Walker
André Walker

Reputation: 633

Insert in sequelize.query and then return the inserted row

Is there a way of returning the inserted row by a raw insert query in sequelize?

The returning option doesn't change anything im the returned value. Is returning the counting of inserted rows and a empty array instead of the inserted rows: [ [], 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,
    }
);

Upvotes: 1

Views: 6049

Answers (1)

hubble12345678
hubble12345678

Reputation: 126

For postgress, you need RETURNING id included in the raw query.

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

Upvotes: 11

Related Questions