Unknown column 'id' in 'field list

Hello,

I’m trying to write a unit test for model that doesn’t have an ‘id’
field. I created the model’s table with the following migration, making
sure the primary key was set.

class CreateActiveUserMetrics < ActiveRecord::Migration
def self.up
create_table :active_user_metrics,
{:id => false,
:primary_key => :sample_date} do |t|
t.column :sample_date, :date
t.column :less_than_thirty_days, :integer
t.column :less_than_ninety_days, :integer
t.column :all_days, :integer
end
end

def self.down
drop_table :active_user_metrics
end
end

The test I wrote is as follows.

class ActiveUserMetricTest < Test::Unit::TestCase
fixtures :active_user_metrics

def test_invalid_with_empty_attributes
metric = ActiveUserMetric.new
assert metric.invalid?
assert metric.errors.invalid?(:sample_date)
assert metric.errors.invalid?(:less_than_thirty_days)
assert metric.errors.invalid?(:less_than_ninety_days)
assert metric.errors.invalid?(:all_days)
end
end

When I run the test I get the following error, complaining about the
lack of an ‘id’ field.

Loaded suite test/unit/active_user_metric_test
Started
EE
Finished in 0.781 seconds.

  1. Error:
    test_invalid_with_empty_attributes(ActiveUserMetricTest):
    ActiveRecord::StatementInvalid: Mysql::Error: #42S22Unknown column ‘id’
    in ‘field list’: INSERT INTO active_user_metrics (id) VALUES (2)
    c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract_adapter.rb:120:in
    log' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/mysql_adapter.rb:184:inexecute’
    c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:288:in
    insert_fixtures' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:287:ineach’
    c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:287:in
    insert_fixtures' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:257:increate_fixtures’
    c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:257:in
    each' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:257:increate_fixtures’
    c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract/database_statements.rb:51:in
    transaction' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:255:increate_fixtures’
    c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:794:in
    silence' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:248:increate_fixtures’
    c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:565:in
    load_fixtures' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:512:insetup’

  2. Error:
    test_invalid_with_empty_attributes(ActiveUserMetricTest):
    NoMethodError: You have a nil object when you didn’t expect it!
    You might have expected an instance of Array.
    The error occured while evaluating nil.-
    c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:112:in
    unlock_mutex' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:534:inteardown’

1 tests, 0 assertions, 0 failures, 2 errors

What should I do to solve this problem? Any help is appreciated.

Thanks,
Justin

Try adding set_primary_key “sample_date” to your ActiveUserMetric model.

Fred

Fred wrote:

Try adding set_primary_key “sample_date” to your ActiveUserMetric model.

I get the same error when I add that to my code.

In the Primary Keys section of AWDWR on page 263 (the latest beta book
version anyway) it sounds like it is just seriously frowned upon to have
a model without ‘id’ defined.

Can anyone else confirm this, or suggest a way I can make this work?

Oh and looking more closely at your error, i’m betting that
active_user_metrics.yml looks like

first:
id: 1

second:
id: 2

Remove those and you should be good to go

Fred

Fred wrote:

Oh and looking more closely at your error, i’m betting that
active_user_metrics.yml looks like

first:
id: 1

second:
id: 2

Remove those and you should be good to go

That fixed it! Thanks!

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