postgres
postgres copied to clipboard
Dynamic columns in updates
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?
WHERE id=${sql(cliente.id)}
to
WHERE id=${cliente.id}
thanks.. damn, i feel stupid.. didn't see i wrapped it up with the sql()..