I know this question has been asked a lot, but I don't find an answer as to why I get this error message with a unique field:
Here are my 2 tables and an index:
CREATE TABLE posts (
id bigint NOT NULL,
user_id bigint NOT NULL,
content text
);
CREATE TABLE users (
id bigint NOT NULL,
email character varying DEFAULT ''::character varying NOT NULL
)
CREATE UNIQUE INDEX index_users_on_email ON users USING btree (email);
The following sql request:
SELECT posts.content, users.email /*, other aggregate fields not relevant for the question */
FROM posts
INNER JOIN users ON posts.user_id = users.id
/* Other `inner join`s but not relevant for the question */
GROUP BY posts.id;
give me the error column "users.email" must appear in the GROUP BY clause or be used in an aggregate function.
But the email field is unique (if it changes anything) and a post can only have one user (so one email).
I don't understand why this request is not valid since it's not possible to have multiple values of email per post.