user16015333
user16015333

Reputation:

How do we use INSERT with WHERE in postgresql

await self.client.db.execute('INSERT INTO config (watcher_channel) VALUES ($1) WHERE guild_id = $2', channel.id, ctx.guild.id)

Im using asyncpg, idk why it gives me syntax error.

Error: Command raised an exception: PostgresSyntaxError: syntax error at or near "WHERE"

Upvotes: 1

Views: 95

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1270623

I am guessing you really want an update:

update config
    set watcher_channel = $1
    where guild_id = $2;

INSERT inserts a complete new row into the table. I suspect that you simply want to update a value in an existing row.

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522292

Rephrase your insert as INSERT INTO ... SELECT:

await self.client.db.execute(
    'INSERT INTO config (watcher_channel) SELECT $1 WHERE guild_id = $2',
    channel.id, ctx.guild.id)

Upvotes: 1

Related Questions