Replies in thread showing out of order

After recently updating to 2.6.0.beta3 some replies in various threads are showing in the wrong order. The time stamps show correctly, but the affected replies are always at the bottom of the thread.

I have tried updating again, and running rake posts:reorder_posts on the affected topics, but this has had no effect. Is there anything else I can do to get them to re-order correctly? Or is this a bug in discourse? The posts display the same for all users.

An example from one of the topics, this is the bottom of the thread:

1 Like

Did you by any chance move the last 2 posts into that topic? It’s the expected behavior in that case.

2 Likes

No, they were always in that topic, and before upgrading they were in the expected order of replies.

This isn’t just in one topic, but in seemingly all of them. There are one or two replies that are at the end that shouldn’t be.

2 Likes

I had the same problem.

No, I don’t move posts into the topic, But replies in thread showing out of order.

Running the query from posts:reorder_posts shows that the sort_order for a number of posts is odd, and looks like the rake should update them, but it does not.


From running:

SELECT
id,
post_number,
sort_order,
ROW_NUMBER() OVER (
    PARTITION BY
    topic_id
    ORDER BY
    created_at,
    post_number
) AS new_post_number
FROM
posts

In trying to manually re-create the rake task, I’ve run:

WITH ordered_posts AS (
  SELECT
    id,
    ROW_NUMBER() OVER (
      PARTITION BY
        topic_id
      ORDER BY
        created_at,
        post_number
    ) AS new_post_number
  FROM
    posts
  /*where*/
)
SELECT
  p.post_number,
  p.sort_order,
  o.new_post_number
FROM
  posts AS p, ordered_posts AS o
WHERE
  p.id = o.id

resulting in:
image

Which shows why the rake task does nothing - it thinks there is nothing to do… The rake task has an additional where comparing post_number to new_post_number, and not sort_order.

I have now run:

WITH ordered_posts AS (
  SELECT
    id,
    ROW_NUMBER() OVER (
      PARTITION BY
        topic_id
      ORDER BY
        created_at,
        post_number
    ) AS new_post_number
  FROM
    posts
  /*where*/
)
UPDATE
  posts AS p
SET
  sort_order = o.new_post_number
FROM
  ordered_posts AS o
WHERE
  p.id = o.id AND
  p.sort_order <> o.new_post_number

on the database manually, and the posts now appear in the correct order.

2 Likes

I still don’t understand how you got into this state, the only place we fiddle with this is when you move posts.

Post creator simply sets sort_order to post_number

https://github.com/discourse/discourse/blob/4ae15285691c62abb2d884cc6a055a0556ef4b33/lib/post_creator.rb#L303-L303

Long term I am not even sure I want to carry sort_order column, it appears to be a footgun of sorts.

Are there any plugins involved here?

I don’t understand it either, I couldn’t find any reason for it when looking through the code. Something seems to modify the sort order, but I can’t see what.

I thought that they had gone out of order when I ran an upgrade, as the messages that were out of order were generally much older, and had previously appeared in an expected place. When I fan the SQL above all seemed well, but then the other week a couple of brand new posts have also now appeared out of order. I think the posts it affects are always replies to another post. eg:
image

We have the following plugins installed:

I guess question-answer modifies the sort order, but that should only affect question topics, not general ones.

I have the same issue. I have previously had 502 errors when moving posts due to long topics, not sure if that has anything to do with it. I also have the question-answer plugin installed but I’m still seeing posts appearing at the bottom of topics after disabling it.

Very likely, this is not really a supported or official plugin, sort_order is only used inconsistently in discourse core, we are considering removing this column.

Moving this to support cause I see this as a bug in a non official plugin.

@angus Can you take a look at this issue? I installed this plugin and replies started to appear out of order for the entire site. I just removed the plugin and replies still appear out of order.

I’ve run a test on a single post to see if the following command will correct the out of order replies in a single post (after removing the Q&A plugin).

rake posts:reorder_posts[1896]

Unfortunately posts are still appearing out of order.

Hey, sorry you’ve experienced this issue. Please post any updates on this in Question Answer Plugin. @mbcahyono and or I will respond shortly. This is not a general Discourse support question.

2 Likes