Rails created_at timestamp order inconsistent with id order

I have a Rails 2.3.5 application with a table containing id and created_at columns. The table captures state changes for entities over time, so I sometimes use it to search for the state of an object at a specific time, by searching for state changes that occurred before time, and selecting the latter according to the created_at timestamp. For 10 out of 1445 objects, timestamps of state changes are in a different order by identifiers, and the state of the last state change is different from the state that is stored with the object itself, for example.

  id  |     created_at      | entity_id | state |
------+---------------------+-----------+-------+
 1151 | 2009-01-26 10:27:02 | 219       | 1     | 
 1152 | 2009-01-26 10:27:11 | 219       | 2     | 
 1153 | 2009-01-26 10:27:17 | 219       | 4     | 
 1154 | 2009-01-26 10:26:41 | 219       | 5     | 

I might get around this by ordering id instead of a timestamp, but I can't come up with an explanation of how this could happen. The application uses several instances of mongrel, but they are all located on the same machine (Debian Lenny); Am I missing something obvious? DB - Postgres.

+5
source share
1 answer

Because Rails uses a database sequence to get a new identifier for your field id(at least in PostgreSQL) on insert or with a keyword RETURNINGif the database supports it.

But it updates the field created_at, and updated_atwhen you create by using ActiveRecord::Timestamp#create_with_timestampsthe method, which uses the system time.

1154 ​​, created_at .

+4

All Articles