# Self-referential HABTM SQL mal-formed when updating record

**URL:** <https://www.ruby-forum.com/t/self-referential-habtm-sql-mal-formed-when-updating-record/126975>\
**Category:** Rails\
**Created:** [January 23, 2008, 10:03pm UTC](https://www.ruby-forum.com/t/self-referential-habtm-sql-mal-formed-when-updating-record/126975 "2008-01-23T22:03:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sazima](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@Sazima](https://www.ruby-forum.com/u/Sazima)\
**Post date:** [January 23, 2008, 10:03pm UTC](https://www.ruby-forum.com/t/self-referential-habtm-sql-mal-formed-when-updating-record/126975/1 "2008-01-23T22:03:26Z")

</div>

Guys,

Anyone ever had this kind of problem with ActiveScaffold?

Cheers, Sazima

* * *

What steps will reproduce the problem?

1. Create self-referential HABTM application (like the common “user  
has friends” example)

2. Try to add more than 1 friend to a given user using ActiveScaffold.

What is the expected output? What do you see instead?  
2+ friends related to the user. Instead I get a MySQL error:

ActiveRecord::StatementInvalid (Mysql::Error: Duplicate entry ‘4’ for  
key  
1: INSERT INTO friends (`updated_at`, `id`, `user_id`, `created_at`,  
`friend_id`) VALUES (‘2008-01-22 20:27:49’, 4, 2, ‘2008-01-22  
20:26:31’, 4))

What version (or revision) of the product are you using?  
1.1.1 + Rails 2.0.2 (InstantRails 2 installation)

---

<div class="post-metadata">

**Author:** ![Sazima](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@Sazima](https://www.ruby-forum.com/u/Sazima)\
**Post date:** [January 24, 2008, 9:35am UTC](https://www.ruby-forum.com/t/self-referential-habtm-sql-mal-formed-when-updating-record/126975/2 "2008-01-24T09:35:51Z")

</div>

friend\_id is a key (ie it must be unique)

this is a DB schema error. not a rails error.  
Just remove the key definition around that column
