# Rake db Fixtures MySql autoincrement problem?

**URL:** <https://www.ruby-forum.com/t/rake-db-fixtures-mysql-autoincrement-problem/173044>\
**Category:** Rails\
**Created:** [July 31, 2009, 11:05pm UTC](https://www.ruby-forum.com/t/rake-db-fixtures-mysql-autoincrement-problem/173044 "2009-07-31T23:05:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BrendanC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9c0ed/32.png) [@BrendanC](https://www.ruby-forum.com/u/BrendanC)\
**Post date:** [July 31, 2009, 11:05pm UTC](https://www.ruby-forum.com/t/rake-db-fixtures-mysql-autoincrement-problem/173044/1 "2009-07-31T23:05:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BrendanC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9c0ed/32.png) [@BrendanC](https://www.ruby-forum.com/u/BrendanC)\
**Post date:** [August 1, 2009, 12:36am UTC](https://www.ruby-forum.com/t/rake-db-fixtures-mysql-autoincrement-problem/173044/2 "2009-08-01T00:36:15Z")

</div>

BrendanC wrote:

> 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?

This is normal behavior (I believe the numbers come from a hash of the  
record, or something like that). Why is it a problem for you?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![BrendanC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9c0ed/32.png) [@BrendanC](https://www.ruby-forum.com/u/BrendanC)\
**Post date:** [August 5, 2009, 3:35pm UTC](https://www.ruby-forum.com/t/rake-db-fixtures-mysql-autoincrement-problem/173044/3 "2009-08-05T15:35:13Z")

</div>

Just tried the same think and I was surprised as you. I do not think  
that it is normal behavior.  
It is really weird. And even sometimes it works as I wish and second  
time (after rake db:fixture:load) not.

Zdenek

On 1 srp, 00:36, Marnen Laibow-Koser \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![BrendanC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9c0ed/32.png) [@BrendanC](https://www.ruby-forum.com/u/BrendanC)\
**Post date:** [August 6, 2009, 1:38pm UTC](https://www.ruby-forum.com/t/rake-db-fixtures-mysql-autoincrement-problem/173044/4 "2009-08-06T13:38:42Z")

</div>

2009/8/5 removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Just tried the same think and I was surprised as you. I do not think  
> that it is normal behavior.  
> It is really weird. And even sometimes it works Â as I wish and second  
> time (after rake db:fixture:load) not.

As Marnen said this is normal behaviour. If an id value is explicitly  
provided for a record in the fixture data then that value will be  
used, otherwise a value is calculated from the name of the record (ie  
the heading line of the record in the fixture). This allows automatic  
linking of associated record. So if A has\_many Bs and B belongs\_to A  
then in A fixture:

a\_1:  
field\_1: data  
field\_2: data

and in B fixture

b\_1:  
a: a\_1 # instead of a\_id: id\_of\_a\_1\_whatever\_that\_is

this makes it much easier to link records than manually setting up all  
the id values

Colin
