Mysql error when no id column

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.

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

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

Fred

[email protected] 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.

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs