jake
jake

Reputation: 1665

Rails Column with ":references" -- Confusion

I have a user table that "belongs_to :user_category", and user_category "has_many :users". I'm trying to set the type to ":references" like so...

class AddUserCategoryToUsers < ActiveRecord::Migration
  def change
    add_column :users, :user_category, :references, :default => 1
  end
end

But it doesn't appear to be working:

==  CreateUsers: migrating ====================================================
-- create_table(:users)
   -> 0.0114s
==  CreateUsers: migrated (0.0115s) ===========================================

==  CreateUserCategories: migrating ===========================================
-- create_table(:user_categories)
   -> 0.0118s
==  CreateUserCategories: migrated (0.0119s) ==================================

==  AddUserCategoryToUsers: migrating =========================================
-- add_column(:users, :user_category, :references, {:default=>1})
rake aborted!
An error has occurred, all later migrations canceled:

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'references DEFAULT 1' at line 1: ALTER TABLE `users` ADD `user_category` references DEFAULT 1

Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Here's the full trace:

-- add_column(:users, :user_category, :references, {:default=>1})
rake aborted!
An error has occurred, all later migrations canceled:

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'references DEFAULT 1' at line 1: ALTER TABLE `users` ADD `user_category` references DEFAULT 1
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `block in execute'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:461:in `add_column'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:466:in `block in method_missing'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:438:in `block in say_with_time'
/Users/jakebuob/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/benchmark.rb:280:in `measure'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:438:in `say_with_time'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:458:in `method_missing'
/Users/jakebuob/Sites/pears/db/migrate/20121114033751_add_user_category_to_users.rb:3:in `change'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:407:in `block (2 levels) in migrate'
/Users/jakebuob/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/benchmark.rb:280:in `measure'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:407:in `block in migrate'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:123:in `with_connection'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:389:in `migrate'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:528:in `migrate'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:777:in `call'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:777:in `ddl_transaction'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:719:in `block in migrate'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:700:in `each'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:700:in `migrate'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:570:in `up'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/migration.rb:551:in `migrate'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:153:in `block (2 levels) in <top (required)>'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/jakebuob/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/bin/rake:19:in `load'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286@global/bin/rake:19:in `<main>'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/bin/ruby_noexec_wrapper:14:in `eval'
/Users/jakebuob/.rvm/gems/ruby-1.9.3-p286/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:migrate

Upvotes: 0

Views: 1573

Answers (3)

Kamal A. Hosny
Kamal A. Hosny

Reputation: 69

To add a reference in a migration you can use this :

class AddUserCategoryToUsers < ActiveRecord::Migration
  def change
    add_reference :users, :user_category, :default => 1
  end
end

Upvotes: 0

Simone Carletti
Simone Carletti

Reputation: 176412

The :references type doesn't exist. You need to create the real column.

class AddUserCategoryToUsers < ActiveRecord::Migration
  def change
    add_column :users, :user_category_id, :integer, :default => 1
  end
end

It's also a good idea to create an index.

class AddUserCategoryToUsers < ActiveRecord::Migration
  def change
    add_column :users, :user_category_id, :integer, :default => 1
    add_index :users, :user_category_id
  end
end

The .references method exists only as part of the table definition (here's an example)

class AddUserCategoryToUsers < ActiveRecord::Migration
  def change
    change_table :users do |t|
      t.references :user_category, :default => 1
    end
  end
end

Upvotes: 2

MrDanA
MrDanA

Reputation: 11647

"References" isn't a column type. You can use the references method when creating/modifying a table but all Rails does is add an integer column for you. What you want instead is to add a column called user_category_id, type integer, and then be sure to set up the associations properly in your models.

Upvotes: 0

Related Questions