postgres icon indicating copy to clipboard operation
postgres copied to clipboard

Dynamic columns in updates

Open rbarszcz opened this issue 1 year ago • 2 comments

trying to make an update with dynamic columns and i'm getting a bunch of errors

object cliente: {

id: 3,
nome: 'Teste Um',
tipo: 'F',
cpf: '12345678910',
cnpj: null,
email: '[email protected]',
telefone: '46111111111',
status: 'A',
datacadastro: '2024-02-01'

}

query

const cols = ['nome', 'tipo', 'cpf', 'cnpj', 'email', 'telefone'];
const update = await sql`UPDATE clientes SET (${sql(cliente, cols)}) WHERE id=${sql(cliente.id)}`;

error: Cannot convert undefined or null to object

query 2 (removed the null fields)

const cols = ['nome', 'tipo', 'cpf', 'email', 'telefone'];
const update = await sql`UPDATE clientes SET (${sql(cliente, cols)}) WHERE id=${sql(cliente.id)}`;

error: syntax error at or near "$1"

query 3 (removed the () after SET)

const cols = ['nome', 'tipo', 'cpf', 'cnpj', 'email', 'telefone'];
const update = await sql`UPDATE clientes SET ${sql(cliente, cols)} WHERE id=${sql(cliente.id)}`;

error: syntax error at end of input

any thoughts?

rbarszcz avatar Feb 01 '24 18:02 rbarszcz

WHERE id=${sql(cliente.id)} to WHERE id=${cliente.id}

lnlife avatar Feb 02 '24 10:02 lnlife

thanks.. damn, i feel stupid.. didn't see i wrapped it up with the sql()..

rbarszcz avatar Feb 02 '24 13:02 rbarszcz