# Mysql error when no id column

**URL:** <https://www.ruby-forum.com/t/mysql-error-when-no-id-column/69517>\
**Category:** Rails\
**Created:** [September 21, 2006, 3:03pm UTC](https://www.ruby-forum.com/t/mysql-error-when-no-id-column/69517 "2006-09-21T15:03:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bradley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ba0ec/32.png) [@Bradley\_M](https://www.ruby-forum.com/u/Bradley_M)\
**Post date:** [September 21, 2006, 3:03pm UTC](https://www.ruby-forum.com/t/mysql-error-when-no-id-column/69517/1 "2006-09-21T15:03:46Z")

</div>

Hi,

I’ve got some information in a new table I’m adding to my system.  
(I’m not a database person, so forgive my lack of correct  
terminology.) I was expecting to access the table via fields other  
than the id field (contribution\_id, user\_id and/or ip\_address), but  
never by id, so I didn’t create the table with the id field (":id =\>  
false" in my create\_table call.

Anyway, it seemed to work find for creating records. But when I went  
to modify an object and save it, I got the following error:

Mysql::Error: #42S22Unknown column ‘id’ in ‘where clause’: UPDATE  
ratings SET `contribution_id` = 27, `user_id` = NULL, `rate_good` = 0,  
`ip_address` = ‘127.0.0.1’ WHERE id = NULL

Now if I change my table definition to have an id field (ie, get rid  
of the “:id =\> false” clause), everything seems to work fine.

Does this sound like a bug, or user-error? If the latter, what am I  
misunderstanding and what is the fix?

## Thanks, Brad

Bradley M.

---

<div class="post-metadata">

**Author:** ![Bradley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ba0ec/32.png) [@Bradley\_M](https://www.ruby-forum.com/u/Bradley_M)\
**Post date:** [September 21, 2006, 6:34pm UTC](https://www.ruby-forum.com/t/mysql-error-when-no-id-column/69517/2 "2006-09-21T18:34:35Z")

</div>

> Active record just really really wants you to have a primary key (which  
> you don’t have to call id)

Then why would create\_table have an “:id =\> _boolean_” parameter?

Should :id be deprecated?

Brad

---

<div class="post-metadata">

**Author:** ![Bradley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ba0ec/32.png) [@Bradley\_M](https://www.ruby-forum.com/u/Bradley_M)\
**Post date:** [September 21, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/mysql-error-when-no-id-column/69517/3 "2006-09-21T16:23:09Z")

</div>

Active record just really really wants you to have a primary key (which  
you don’t have to call id)

Fred

---

<div class="post-metadata">

**Author:** ![Bradley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ba0ec/32.png) [@Bradley\_M](https://www.ruby-forum.com/u/Bradley_M)\
**Post date:** [September 21, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/mysql-error-when-no-id-column/69517/4 "2006-09-21T18:42:40Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > Active record just really really wants you to have a primary key (which  
> > you don’t have to call id)
> 
> Then why would create\_table have an “:id =\> _boolean_” parameter?
> 
> Should :id be deprecated?
> 
> Brad

because by default, the create table will create an “id” field all by  
itself. So, you have to set that parameter to ‘false’ if you do not  
want that id field to be created (in which case, you will be required to  
put in a field that serves as the primary key if you want one)

hope this helps,  
Cheers  
Mohit.

---

<div class="post-metadata">

**Author:** ![Bradley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ba0ec/32.png) [@Bradley\_M](https://www.ruby-forum.com/u/Bradley_M)\
**Post date:** [September 21, 2006, 11:01pm UTC](https://www.ruby-forum.com/t/mysql-error-when-no-id-column/69517/5 "2006-09-21T23:01:21Z")

</div>

unknown wrote:

> If that were the case, then wouldn’t the assignment of the alternative  
> primary key field be a sufficient indicator for the primary key  
> alternative?
> 
> Brad

If you are using something other than id as your primary key you need to  
tell activerecord, ie

class Widget \< ActiveRecord::Base  
set\_primary\_key “widget\_id”  
end

The ability to turn off the the id column is also useful for join  
tables.

Fred

---

<div class="post-metadata">

**Author:** ![Bradley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ba0ec/32.png) [@Bradley\_M](https://www.ruby-forum.com/u/Bradley_M)\
**Post date:** [September 21, 2006, 8:04pm UTC](https://www.ruby-forum.com/t/mysql-error-when-no-id-column/69517/6 "2006-09-21T20:04:55Z")

</div>

If that were the case, then wouldn’t the assignment of the alternative  
primary key field be a sufficient indicator for the primary key  
alternative?

Brad
