带有“:references”的Rails列 - 混乱

时间:2012-11-14 15:32:18

标签: mysql ruby-on-rails activerecord

我有一个用户表“belongs_to:user_category”和user_category“has_many:users”。我正在尝试将类型设置为“:references”,如此...

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

但它似乎没有起作用:

==  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)

以下是完整的跟踪:

-- 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

3 个答案:

答案 0 :(得分:2)

:references类型不存在。您需要创建真正的列。

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

创建索引也是个好主意。

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

.references方法仅作为表定义(here's an example

的一部分存在
class AddUserCategoryToUsers < ActiveRecord::Migration
  def change
    change_table :users do |t|
      t.references :user_category, :default => 1
    end
  end
end

答案 1 :(得分:0)

“引用”不是列类型。您可以在创建/修改表时使用references方法,但所有Rails都会为您添加整数列。你想要的是添加一个名为user_category_id的列,输入整数,然后确保在模型中正确设置关联。

答案 2 :(得分:0)

要在迁移中添加参考,可以使用以下方法:

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