0

I have a postgres database that contains a table of 'jobs'. On my client, I am mapping through these jobs and listing their title. When a user makes a request to update a property for one of the job and I reload the list, the altered job is located at the end of the array? What is happening to cause this and is there any way to keep them from reordering?

wezrine
  • 73
  • 1
  • 2
  • 8
  • This question is vague, because it contains no details or information about the SQL you're using, the schema of any tables, and a very unclear problem description. The ordering of data is controlled by an ORDER BY clause. If you're getting the wrong results, you're using the wrong ORDER BY, or you're not using one at all, in which case the database engine can display them in whatever order it wants. – Ken White Sep 21 '21 at 02:50
  • @wezrine, you have asked several questions in the past but haven't accepted any answer. I'd highly encourage to accept an answer of your liking to all your asked questions that have answered. That'll put closure to your questions and it'll help others looking for a similar question find yours with an answer. – zedfoxus Sep 21 '21 at 02:59

1 Answers1

1

You should always use an order by clause in your query to list the jobs in a particular order. Otherwise, there is no guaranty that records will be ordered in the same way each time you query.

In your your application, update your query something like this select title, field2 from jobs where client = 10 order by job_id. Then, loop through the jobs. When they alter the job, and let's say they altered job id 4 of 10, requery their data using the order by clause. That'll give the same ordering over and over again.

zedfoxus
  • 35,121
  • 5
  • 64
  • 63
  • 3
    Good answer. I would add a mention that it is the relational model that says rows have no order unless you specify one, not a Postgres-specific thing. – Basil Bourque Sep 21 '21 at 02:54