Topic: find returns 'undefined column' SQL error in Rails 3

I am having trouble resolving an error as I convert my app from Rails 2.3.x to 3.2.4. Execution of the 'find' method for some models results in an 'Unknown column' SQL error. I'm using MYSQL2.

Executing this code:
    @album = Album.find(params[:id])

Results in this error:
    Mysql2::Error: Unknown column 'albums.' in 'where clause': SELECT  `albums`.* FROM `albums`  WHERE `albums`.`` = '425' LIMIT 1

Notice the column name 'id' is missing in the where clause. I can avoid the error if I add the ':first' option to the find.

This works:
   @album = Album.find(:first, params[:id])

Now I'm getting the same error when updating the record. The following code will fail with the same SQL error as above and I haven't been able to find a solution.

Executing this code:
    if @album.update_attributes(params[:album])
      flash[:notice] = 'Successfully updated Album.'
      redirect_to @album
    else
      render :action => 'edit'
    end

Results in this error:
Mysql2::Error: Unknown column 'albums.' in 'where clause': UPDATE `albums` SET `title` = 'Abacab', `group_id` = 162, `visual_id` = 672, `editors_comments` = '1981 Abacab * Released: 18 September 1981', `tracks` = ' ', `album_info` = '', `release_date` = '1981-09-18', `display_start_date` = NULL, `display_end_date` = NULL, `updated_at` = '2012-07-14 03:59:23' WHERE `albums`.`` = 2

Again the column name 'id' is missing in the where clause. I would appreciate any help in resolving this issue.

Thanks,
Ken

Last edited by krkoceja (2012-07-14 00:09:04)

Re: find returns 'undefined column' SQL error in Rails 3

I was able to resolve this issue. I noticed in the DB schema file that the models that exhibited this problem appeared in the schema as not having a primary key. These tables have the id as a unique key in the database, but not as the primary key. Once I set the id columns in the DB to primary key and dumped the schema with 'rake db:schema:dump', the problem was resolved.

- Ken

Re: find returns 'undefined column' SQL error in Rails 3

Thanks for sharing. This saved me hours of work wink