title: PostgreSQL json_agg, and trying to group by and order by (column must appear in GROUP BY)
body: <p>I've read several of the stackoverflow's about this error, but I still don't get it.</p>
  
  <pre><code>-- Returns full JSON document of a user, and all their user_files.
  with user_files_json as (
      select user_id,
             json_agg(
               json_build_object(
                 'user_file_id', user_file_id,
                 's3_key', s3_key,
                 'settings', series_settings
               )
             ) as user_files
        from user_files
       where user_id = :user_id
    group by user_id
    order by user_file_id asc
  )
  select json_build_object(
           'user_id', user_id,
           'user_name', user_name,
           'job_type_name', job_type_name,
           'user_files', user_files
         )
    from user_files_json
    join users using(user_id)
    join job_type using(job_type_id);
  </code></pre>
  
  <p>I want the files to list in order by their user_file_id. That's all. If I take out the <code>order by</code> things work, but files come out in the wrong order.</p>
  
  <p>But I get an error saying: </p>
  
  <pre><code>column "user_files.user_file_id" must appear in the GROUP BY clause or be used in an aggregate function
  </code></pre>
  
  <p>What I'm telling postgresql makes perfect sense to me as a human, but can someone explain to me what I'm doing wrong and how to fix it?</p>