# Rake vs Ruby for running tests (error discrepency)

**URL:** <https://www.ruby-forum.com/t/rake-vs-ruby-for-running-tests-error-discrepency/64349>\
**Category:** Rails\
**Created:** [June 24, 2006, 5:54pm UTC](https://www.ruby-forum.com/t/rake-vs-ruby-for-running-tests-error-discrepency/64349 "2006-06-24T17:54:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [June 24, 2006, 5:54pm UTC](https://www.ruby-forum.com/t/rake-vs-ruby-for-running-tests-error-discrepency/64349/1 "2006-06-24T17:54:18Z")

</div>

I’m having (to me) a strange problem with errors when running my tests  
with rake as opposed to using ruby. If I do rake test:units I get this  
error for several tests, but not all:

1. Error:  
test\_player\_has\_game\_statistics\_for\_season(PlayerSeasonTest):  
ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry ‘22’ for  
key 1: INSERT INTO positions (`name`, `id`, `position`) VALUES  
(‘athlete’, ‘22’, ‘ATH’)  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/connection\_adapters/abstract\_adapter.rb:120:in  
`log' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connection_adapters/mysql_adapter.rb:185:in`execute’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/fixtures.rb:288:in  
`insert_fixtures' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/fixtures.rb:287:in`insert\_fixtures’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/fixtures.rb:257:in  
`create_fixtures' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/fixtures.rb:257:in`create\_fixtures’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/connection\_adapters/abstract/database\_statements.rb:51:in  
`transaction' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/fixtures.rb:255:in`create\_fixtures’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/base.rb:794:in  
`silence' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/fixtures.rb:248:in`create\_fixtures’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/fixtures.rb:565:in  
`load_fixtures' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/fixtures.rb:512:in`setup\_with\_fixtures’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/fixtures.rb:547:in  
`setup’

However if I use “ruby test/unit/player\_season\_test.rb” I don’t get any  
errors. Same thing happens with funtional tests. Using rake to run the  
tests gives me this error about duplicate keys but running the  
functionals individually with Ruby passes fine. Any  
hints/suggestions/solutions? Seems like some kind of issue with the  
fixtures but I have no idea what I can or should be doing here. Thanks  
for any help.

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [June 25, 2006, 12:33am UTC](https://www.ruby-forum.com/t/rake-vs-ruby-for-running-tests-error-discrepency/64349/2 "2006-06-25T00:33:45Z")

</div>

I had this issue too. Turned out my user model was calling my country  
model somewhere (though nowhere I could think of – although it did in  
the main layout – go figure!). I added the countries fixtures to the  
tests and it sorted the prob. Suspect something similar is happening  
with you. Not sure why there’s the discrepancy. Please post back if you  
ever find out…

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [June 25, 2006, 4:19pm UTC](https://www.ruby-forum.com/t/rake-vs-ruby-for-running-tests-error-discrepency/64349/3 "2006-06-25T16:19:49Z")

</div>

Chris T wrote:

> I had this issue too. Turned out my user model was calling my country  
> model somewhere (though nowhere I could think of – although it did in  
> the main layout – go figure!). I added the countries fixtures to the  
> tests and it sorted the prob. Suspect something similar is happening  
> with you. Not sure why there’s the discrepancy. Please post back if you  
> ever find out…

I did have that as a similar problem but with different errors. I  
finally figured that out and had added the fixtures that were needed for  
the association. This is just stranger because it looks like its trying  
to load the fixtures and can’t because of the ID being a primary key.  
However the positions table (table/fixture in question here) are empty  
to begin with. I’ve even tried to preload them but using rake they just  
get zapped anyway. Admittedly my knowledge of how the rake task for  
running tests works is low as is my knowledge of fixtures beyond making  
them correctly formatted yaml and calling them in my tests. This just  
seems like a potential bug but I can’t even figure how or what exactly  
could be happening. My understanding is that loading a fixture removes  
any prexisting data in the table before loading it so this error  
shouldn’t occur. It’s not like it’s a foreign key error its a primary  
key error and I’ve double checked that the fixture doesn’t have  
duplicate entries.

Something I noticed is that if I have say 3 of this error they will  
progress through the IDs:

1. Error:  
ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry ‘18’ for  
key 1:

2. Error:  
ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry ‘19’ for  
key 1:

3. Error:  
ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry ‘20’ for  
key 1:

and so on. Strange that it always ascends through the ID’s. Maybe  
that’s a clue.

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [June 25, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/rake-vs-ruby-for-running-tests-error-discrepency/64349/4 "2006-06-25T17:42:28Z")

</div>

On Jun 24, 2006, at 10:54 AM, Paul C. wrote:

> tests gives me this error about duplicate keys but running the  
> functionals individually with Ruby passes fine. Any  
> hints/suggestions/solutions? Seems like some kind of issue with the  
> fixtures but I have no idea what I can or should be doing here.  
> Thanks  
> for any help.

Take a look at your config/environment.rb file. Do you have a line  
that looks like this?

config.active\_record.schema\_format = :ruby

If so, change “:ruby” to “:sql” and try again. The problem you are  
having **might** be related to the lack of transactional support in  
MySQL’s default database engine. If you overrode your engine choice  
to use InnoDB, the ruby schema format doesn’t retain this  
characteristic when reloading your test database (via the test:\*  
commands).

Anyway, check it out.

cr

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [February 13, 2007, 7:43pm UTC](https://www.ruby-forum.com/t/rake-vs-ruby-for-running-tests-error-discrepency/64349/5 "2007-02-13T19:43:03Z")

</div>

Nice one Chris T.  
I think my problem was closer to Chris’ one than the one of this post  
(no MySQL error for me).  
But same effect: test run with ruby, ok. Same test run with rake  
test:units, failed!  
I did add a missing fixture/model to the test and it passed!  
Thanks again.

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [January 12, 2009, 1:32am UTC](https://www.ruby-forum.com/t/rake-vs-ruby-for-running-tests-error-discrepency/64349/6 "2009-01-12T01:32:56Z")

</div>

The thread is old, however we got similar error recently with rails 2  
and mysql 5.1. It was related to missing fixtures, however the problem  
with mysql

our installation is mysql 5.1.22 on CentOS 5 and 5.1.30 on Debian  
we were able to reproduce the error with simple seqence of sql commands

select version();

DROP TABLE IF EXISTS test\_inc;  
CREATE TABLE `test_inc` (  
`id` int(11) NOT NULL AUTO\_INCREMENT,  
`account_id` int(11) NOT NULL,  
PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

select ‘insert explicit 9. must become 10’;  
insert into test\_inc ( id, account\_id) values (9,1);  
select \* from test\_inc;  
show create table test\_inc \G  
select ‘delete all. stays 10’;  
delete from test\_inc;  
select \* from test\_inc;  
show create table test\_inc \G

select ‘insert autoincrement. must become 11’;  
insert into test\_inc ( account\_id) values (1) ;  
select \* from test\_inc ;  
show create table test\_inc \G  
select ‘delete all. stays 11’;  
delete from test\_inc ;  
select \* from test\_inc ;  
show create table test\_inc \G

select ‘insert explicit 11. must become 12, but stays 11’;  
insert into test\_inc ( id, account\_id) values (11,1);  
select \* from test\_inc;  
show create table test\_inc \G

/\*  
select ‘insert explicit 12. must become 13’;  
insert into test\_inc ( id, account\_id) values (12,1);  
select \* from test\_inc;  
show create table test\_inc \G  
\*/

select \* from test\_inc;  
show create table test\_inc \G  
select ‘insert auto-increment’;  
insert into test\_inc ( account\_id) values (1);  
select \* from test\_inc;  
show create table test\_inc \G

I’ve opened a bug with mysql, however they ‘closed’ it as unable to  
reproduce

[http://ep.blogware.com/blog/\_archives/2009/1/9/4051185.html](http://ep.blogware.com/blog/_archives/2009/1/9/4051185.html)

[http://bugs.mysql.com/bug.php?id=41984](http://bugs.mysql.com/bug.php?id=41984)
