Search code examples
ruby-on-railsunit-testingshoulda

Shoulda tests failing on model with no id


I created a new model in my rails app. Since it's a one-to-one relation with another table, there's no need for the new model to have an id column. Everything is working fine, but for some reason, all of my Shoulda tests on this model are failing. For example:

should_validate_presence_of :first_name

is throwing this error:

ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'my_new_table.id'
in 'field list': SELECT `my_new_table`.id FROM `my_new_table` WHERE 
(`my_new_table`.`some_other_column` IS NULL)  LIMIT 1

Does Shoulda require an ID column? If so, is there a way around this?


Solution

  • Did you alias the primary key for Rails purposes?

    set_primary_key :my_fk_id