I've being trying to update a row with a value from another table but I keep getting the same error:
table name "chats" specified more than once
I'm trying to insert a row into the messages table, and then use that timestamp (message_timestamp) to update the last_updated field in the chats table.
Any help would be appreciated! Been stuck on this for a day now:
WITH result AS
(INSERT INTO messages (user_id, chat_id, message_timestamp, users_read, message_text)
VALUES ($1, $2, NOW(), '{}', $3) RETURNING message_timestamp, chat_id)
UPDATE chats SET chats.last_updated=result.message_timestamp FROM result, chats WHERE chats.id=result.chat_id;
Edit:
On the other hand, removing chats from my FROM clause as so:
UPDATE chats SET chats.last_updated=result.message_timestamp FROM result WHERE chats.id=result.chat_id;
results in a different error:
column "chats" of relation "chats" does not exist
Which is weird considering I never call chats.chats
Edit 2: Create statement for the chat table:
CREATE TABLE IF NOT EXISTS chats (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
chat_name VARCHAR,
last_message TIMESTAMP NOT NULL
);
UPDATE chats SET last_updated=result.message_timestamp FROM result WHERE chats.id=result.chat_id;