# Acts\_as\_tree test fails : duplicate entry # for key #

**URL:** <https://www.ruby-forum.com/t/acts-as-tree-test-fails-duplicate-entry-for-key/184994>\
**Category:** Rails\
**Created:** [March 11, 2010, 3:52pm UTC](https://www.ruby-forum.com/t/acts-as-tree-test-fails-duplicate-entry-for-key/184994 "2010-03-11T15:52:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![steve.gooberman-hil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c6cbf5/32.png) [@steve.gooberman-hil](https://www.ruby-forum.com/u/steve.gooberman-hil)\
**Post date:** [March 11, 2010, 3:52pm UTC](https://www.ruby-forum.com/t/acts-as-tree-test-fails-duplicate-entry-for-key/184994/1 "2010-03-11T15:52:44Z")

</div>

Hi,  
I’ve set up a model as follows

class EquipmentPrice \< ActiveRecord::Base

acts\_as\_tree :order =\> “id”

…  
end

which has the following db migration  
class CreateEquipmentPrices \< ActiveRecord::Migration  
def self.up  
create\_table :equipment\_prices do |t|  
t.integer :id  
t.string :manufacturer  
t.string :model  
t.string :equipment\_type  
t.integer :capacity  
t.integer :string\_size  
t.string :cost\_structure  
t.string :currency  
t.decimal :price\_each  
t.decimal :price\_bulk  
t.integer :bulk\_quantity  
t.integer :parent\_id

```
  t.timestamps
end

```

end

def self.down  
drop\_table :equipment\_prices  
end  
end

But when I try and run the default test, I get an error message  
stating  
ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry ‘1’ for  
key 1: INSERT INTO `equipment_prices` (`id`, `manufacturer`, `model`,  
`equipment_type`, `capacity`, `string_size`, `cost_structure`,  
`currency`, `price_each`, `price_bulk`, `bulk_quantity`, `parent_id`,  
`created_at`, `updated_at`) VALUES (1, NULL, NULL, ‘PV\_subsystem’,  
NULL, NULL, NULL, ‘USD’, 0.0, 0.0, 1, 0, ‘2010-03-11 12:14:05’,  
‘2010-03-11 12:14:05’)

I have read in an earlier post that this is because I have parent\_id  
set as the primary key for the table. But when I look at the mysql  
table definition I see

mysql\> describe equipment\_prices;  
±---------------±--------------±-----±----±--------  
±---------------+  
| Field | Type | Null | Key | Default |  
Extra |  
±---------------±--------------±-----±----±--------  
±---------------+  
| id | int(11) | NO | PRI | NULL |  
auto\_increment |  
| manufacturer | varchar(255) | YES | | NULL  
| |  
| model | varchar(255) | YES | | NULL  
| |  
| equipment\_type | varchar(255) | YES | | NULL  
| |  
| capacity | int(11) | YES | | NULL  
| |  
| string\_size | int(11) | YES | | NULL  
| |  
| cost\_structure | varchar(255) | YES | | NULL  
| |  
| currency | varchar(255) | YES | | NULL  
| |  
| price\_each | decimal(10,0) | YES | | NULL  
| |  
| price\_bulk | decimal(10,0) | YES | | NULL  
| |  
| bulk\_quantity | int(11) | YES | | NULL  
| |  
| parent\_id | int(11) | YES | | NULL  
| |  
| created\_at | datetime | YES | | NULL  
| |  
| updated\_at | datetime | YES | | NULL  
| |  
±---------------±--------------±-----±----±--------  
±---------------+  
14 rows in set (0.00 sec)

which appears to show that id is the primary key, as I would expect.

What am I doing wrong here please?

Thanks

Steve

---

<div class="post-metadata">

**Author:** ![steve.gooberman-hil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c6cbf5/32.png) [@steve.gooberman-hil](https://www.ruby-forum.com/u/steve.gooberman-hil)\
**Post date:** [March 12, 2010, 1:19pm UTC](https://www.ruby-forum.com/t/acts-as-tree-test-fails-duplicate-entry-for-key/184994/2 "2010-03-12T13:19:29Z")

</div>

resolved - it would appear that MySQL treats an int of 0 = NULL. , so  
my first fixture (the root of the tree) which had an id of 0 in the  
fixtures file, was being taken as NULL (stated as not allowed in the  
table definition), so set to id=1 by MySQL, then causing the 2nd  
fixture (id=1 in the fixtures file) to appear as a duplicate.

On Mar 11, 2:51 pm, “steve.gooberman-hill” \<steve.gooberman-h…@power-
