# Code runs in development but not in test

**URL:** <https://www.ruby-forum.com/t/code-runs-in-development-but-not-in-test/167497>\
**Category:** Rails\
**Created:** [May 8, 2009, 3:33pm UTC](https://www.ruby-forum.com/t/code-runs-in-development-but-not-in-test/167497 "2009-05-08T15:33:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [May 8, 2009, 3:33pm UTC](https://www.ruby-forum.com/t/code-runs-in-development-but-not-in-test/167497/1 "2009-05-08T15:33:43Z")

</div>

I have some model code that runs fine in development environment but  
fails in the test environment.

In test, the code always fails at the same point with:

Mysql::Error: Duplicate entry ‘5-17’ for key ‘PRIMARY’: INSERT INTO  
`accessibilities_namespaces` (`accessibility_id`, `namespace_id`)  
VALUES (17, 5) [0m

accessibilities\_namespaces is a join table for HABTM relationships.

My logging suggests that ActiveRecord tries to insert the same record  
twice when run in test (but only in test!) which would explain the  
above error. I am sure that the code does not try to create the same  
record twice and in any case it runs fine in development mode.

I have tried running the code using ‘script/console test’ and it fails  
here also. Though it does not fail when run in ‘script/console  
[development]’.

I suspect the problem does not lie with MySQL or ActiveRecord, but  
something to do with the differences between running in development vs  
test.

Any thoughts appreciated. I’m baffled by this.

---

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [May 8, 2009, 3:54pm UTC](https://www.ruby-forum.com/t/code-runs-in-development-but-not-in-test/167497/2 "2009-05-08T15:54:16Z")

</div>

Is it failing while running one of your tests (possibly an error in the  
test) or while preparing for the test (possibly a problem with your  
fixtures)?

Colin

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

---

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [May 8, 2009, 4:03pm UTC](https://www.ruby-forum.com/t/code-runs-in-development-but-not-in-test/167497/3 "2009-05-08T16:03:39Z")

</div>

Lee -

On 8-May-09, at 9:53 AM, Colin L. wrote:

> Is it failing while running one of your tests (possibly an error in  
> the test) or while preparing for the test (possibly a problem with  
> your fixtures)?
> 
> Colin

to build on Colin’s comment about your fixtures, if you’re relying on  
the testing framework to wrap your tests in transactions, the database  
must support them - mysql myisam database tables do not -

ie. if you’re using myisam, your tables will not be cleaned out after  
every test - and you’d see this behavior

J

---

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [May 8, 2009, 4:11pm UTC](https://www.ruby-forum.com/t/code-runs-in-development-but-not-in-test/167497/4 "2009-05-08T16:11:19Z")

</div>

Lee is using InnoDB.

Chris.

---

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [May 8, 2009, 4:14pm UTC](https://www.ruby-forum.com/t/code-runs-in-development-but-not-in-test/167497/5 "2009-05-08T16:14:50Z")

</div>

Initially it was failing when running the code from within a Cucumber  
step definition. In this step definition, I am implementing a Given  
step (in part) by calling a method of a model. This method is throwing  
the error. In summary, the step definition is as follows:

Given … do  
user = User.create!(…)  
member=Member.add\_member\_for\_user(user) \<----! error thrown by this  
method  
…  
end

Having experienced the error within Cucumber, I then entered the same  
code into ‘script/console test’ i.e.

> > user=User.create!(…)  
> > member=Member.add\_member\_for\_user etc…

The code raised an error again.

If however I run the code in development or by using the development  
console or as a standalone Ruby program which makes use of  
ActiveRecord and my models, it runs fine.

---

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [May 9, 2009, 8:42am UTC](https://www.ruby-forum.com/t/code-runs-in-development-but-not-in-test/167497/6 "2009-05-09T08:42:55Z")

</div>

The actual error that I receive when run in ‘console test’ is a  
‘Duplicate key’ error when trying to insert into one of the join  
(HABTM) tables. I stress this error is not raised when run in ‘console  
development’ so I know that my code is not trying to insert duplicate  
rows in this table.

The underlying MySQL table for this join table has a composite PRIMARY  
KEY defined for the two foreign keys that make up the HABTM  
relationship.

If I remove the PRIMARY KEY from the table definition, the code runs  
fine in ‘console test’.

What does ‘console test’ do differently to ‘console development’ to  
cause this problem?

---

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [May 9, 2009, 2:40pm UTC](https://www.ruby-forum.com/t/code-runs-in-development-but-not-in-test/167497/7 "2009-05-09T14:40:36Z")

</div>

When I remove the primary key from the join table the code runs  
without error using console test but does create two records for each  
pair of keys in the join table. The same code does not however create  
pairs of records when run using console development. So something  
strange is happening in the Rails test environment!

My googling suggests it’s a bad idea to have composite primary keys on  
join tables.

---

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [May 9, 2009, 10:01am UTC](https://www.ruby-forum.com/t/code-runs-in-development-but-not-in-test/167497/8 "2009-05-09T10:01:57Z")

</div>

The error would suggest there is already a record with that composite  
key in  
the database. Have you tried looking at the db just before the error  
occurs  
(or after it for that matter) to see if there is already such a record?

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