Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax Error on Dynamic multiple updates in one query giving #953

Open
arrafi-ahmed opened this issue Sep 27, 2024 · 0 comments
Open

Syntax Error on Dynamic multiple updates in one query giving #953

arrafi-ahmed opened this issue Sep 27, 2024 · 0 comments

Comments

@arrafi-ahmed
Copy link

Anyone can help finding the issue? Getting syntax error on this query:

exports.updateMatches = async ({ payload: { matches } }) => {
  const formattedMatches = objArrToArr(matches);
  const cols = Object.keys(matches[0]);
  const colsWOId = cols
    .filter((item) => item !== "id")
    .map((item) => camelToSnake(item));
    
  return await sql`
        update matches
        set ${colsWOId.map((col) => `${sql(col)} = excluded.${sql(col)}`).join(", ")}
        from (values ${sql(formattedMatches)}) as excluded(${cols.join(", ")})
        where matches.id = (excluded.id):: int
            returning *;`;
};

tried to follow the sql format from official documentation:

const users = [
  [1, 'John', 34],
  [2, 'Jane', 27],
]
await sql`
  update users set name = update_data.name, age = (update_data.age)::int
  from (values ${sql(users)}) as update_data (id, name, age)
  where users.id = (update_data.id)::int
  returning users.id, users.name, users.age
`            
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant