I am using postgreSQL
for storing chat logs, and I have this sample schema:
CREATE TABLE contacts (
"id" BIGSERIAL PRIMARY KEY,
"user" BIGINT NOT NULL,
"contact" BIGINT NOT NULL,
"savedAs" VARCHAR(36),
CONSTRAINT user_fk FOREIGN KEY("user") REFERENCES users("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT contact_fk FOREIGN KEY("contact") REFERENCES users("id") ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE("user", "contact")
);
CREATE TABLE messages (
"id" BIGSERIAL PRIMARY KEY,
"contact" BIGINT NOT NULL,
"direction" direction_type NOT NULL,
"type" message_type default 'text',
"body" VARCHAR(1000) NULL,
"status" status_type DEFAULT 'none',
"time" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT contact_fk FOREIGN KEY("contact") REFERENCES contacts("id") ON DELETE CASCADE
);
CREATE TABLE last_message (
"id" BIGSERIAL PRIMARY KEY,
"chat" BIGINT NOT NULL UNIQUE,
"message" BIGINT NOT NULL,
CONSTRAINT message_fk FOREIGN KEY("message") REFERENCES messages("id"),
CONSTRAINT chat_fk FOREIGN KEY("chat") REFERENCES contacts("id") ON DELETE CASCADE
);
What I want to do, is store the last message for a particular chat in the last_message
table. I was thinking of doing it like this(but not working):
INSERT INTO last_message (chat, message) VALUES (
9,
(INSERT INTO messages (contact, direction, body) VALUES (9, 'sent', 'hello there') RETURNING id)
)
But I get a syntax error(syntax error at or near "into"
), so here are my questions,
what is wrong with the above query?
is there a better a way to do this? how?
is there anything that can be improved?