forked from mtusi2026/mtusi2026lab2
19 lines
634 B
SQL
19 lines
634 B
SQL
CREATE TABLE IF NOT EXISTS messages
|
|
(
|
|
id UUID PRIMARY KEY,
|
|
chat_id UUID NOT NULL,
|
|
sender_id UUID NOT NULL,
|
|
text VARCHAR(4000) NOT NULL,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
|
|
CONSTRAINT messages_text_not_blank CHECK (text !~ '^[[:space:]]*$'),
|
|
CONSTRAINT messages_sender_member_fk FOREIGN KEY (chat_id, sender_id)
|
|
REFERENCES chat_members (chat_id, user_id) ON DELETE NO ACTION
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_messages_chat_id_created_at_id ON messages (chat_id, created_at, id);
|
|
|
|
---- create above / drop below ----
|
|
|
|
DROP TABLE IF EXISTS messages;
|