Skip to content

Cannot use Set to increment column value #622

@danxcraig

Description

@danxcraig

This issue was encountered while attempting to use Set as part of an UPDATE to a PostgreSQL database table.

In order to increment the value of an INT or BIGINT in a column in PostgreSQL, I can use:

UPDATE some_table
   SET some_column = some_column + 1
WHERE some_other_column = 'change me';

The challenge is that Bun's Set always wraps single quotes around it's second parameter. Thus using Set("some_column = ?", "some_column + 1") in code becomes SET some_column = 'some_column + 1' in the UPDATE statement, which subsequently fails with an invalid input syntax for type error where the column is INT or BIGINT.

I'm assuming I'm missing something, so I'd appreciate pointers around the recommended approach for incrementing or decreasing column int values as part of an UPDATE.

Update
As a temporary measure I'm using a separate SELECT statement to scan the existing INT from the table into a currentValue var, incrementing that by one, then using that to setting the column value. It works, but it's not very elegant or efficient.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions