I'm trying to update a table that is linked to another table through an array type column. My question is, is it possible to run a join during an update in Postgres?
Here's what I have:
CREATE SCHEMA IF NOT EXISTS my_schema;
CREATE TABLE IF NOT EXISTS my_schema.table_a (
id serial PRIMARY KEY,
table_b_ids BIGINT[] NOT NULL,
table_a_name VARCHAR(50)
);
CREATE TABLE IF NOT EXISTS my_schema.table_b (
id serial PRIMARY KEY,
tag_value BIGINT UNIQUE NOT NULL
);
INSERT INTO my_schema.table_b VALUES
(1, 12345),
(2, 12346);
INSERT INTO my_schema.table_a VALUES
(1, '{1}', 'Michael');
I need to be able to update table_a
like this:
UPDATE my_schema.table_a
SET table_a_name = 'John'
WHERE 12345 = ANY(table_b_ids)
RETURNING *
Instead of,
. . .
WHERE 1 = ANY(table_b_ids)
. . .
Is this possible?