Defect #44579
openAttachments added in a single update may be listed in the wrong order in the issue history on PostgreSQL
Description
When two or more files are attached to an issue in a single update, the issue history may list them in a different order from the one in which they were attached. I observed this on an instance running PostgreSQL: the "Files" section of the issue showed the two attachments in the correct order, while the corresponding journal listed them, together with their thumbnails, in the reverse order.
The cause appears to be that the details association of the Journal model has no explicit order. The journal details are saved in the order in which the files were attached, but they are loaded without an ORDER BY clause, and PostgreSQL does not guarantee that rows are returned in insertion order in that case. For example, a row inserted later can be stored physically before an earlier one when space freed by deleted rows is reused. The "Files" section is not affected because the attachments association is explicitly ordered by created_on and id. I have not been able to reproduce the problem on demand, because it depends on how PostgreSQL physically stores the rows.
The attached patch adds order(:id) to the details association, so that journal details are always returned in the order in which they were added. This applies to every place where the details of a journal are displayed, not only to attachments. The patch does not include a test. Under normal test conditions, newly inserted rows are returned in insertion order on all supported databases, including PostgreSQL, so a test that adds journal details and checks their order would pass with or without the patch.
Journal:
"Files" section:
Files
No data to display