views:

468

answers:

1

I'm trying to load some Rake Fixtures (rake db:fixtures:load) into a MySql database and I'm seeing some weird behaviour with AutoIncrement values. Normally this goes up by 1 for each insert which allows me to define/create tests. (BTW - normal create/insert from script works correctly).

However when I load from fixtures the id field is assigned a large random number and the autoinc value on the table is also a large number (1054583385) after the load. Has anyone else seen this?

FWIW this is on Windows XP with MySql 5.0 (I also tested with MySql 5.1, found the problem and rolled back to 5.0).

Anybody else seen this - Is this a known bug/issue?

TIA,

+1  A: 

This is not abnormal behavior for rails fixtures. It is, by design a random hash based on the label of your fixture. See the documentation.

You can explicitly specify an ID in your fixtures if needed.

id: 1

But does it really matter? Fixtures are meant to be used for tests. The ID of your objects is irrelevant as long as the relations are there.

Here is the relevant function from the Fixtures class:

# Returns a consistent identifier for +label+. This will always 
# be a positive integer, and will always be the same for a given
# label, assuming the same OS, platform, and version of Ruby.
def self.identify(label)
  label.to_s.hash.abs
end
hobodave
Thx for the info - I'm still learning. Since I'm trying to define/create parent/child/grandchild data relationships for my test, it does matter in this case. Maybe there is a better (more Rails-ish way) to do this - but I do need to create data with specific relationships (I'm testing some report drill down queries). Hence I wanted to be able to predict the fk values in advance.
BrendanC
If your has and belongs relationships are properly defined, you dont ever need to worry about the foreign key values. Use the tools ActiveRecord provides.
hobodave