添加具有多个选项的列时出错

时间:2015-02-26 15:06:38

标签: ruby-on-rails ruby-on-rails-3.2 rails-activerecord

我正在处理的系统中进行以下迁移:

class CreateEstoqueGruposProdutos < ActiveRecord::Migration
  def change
    create_table :est_grupos_produtos do |t|
      t.integer :grupos_produtos_nivel_id, :null => false, foreign_key: { references: :est_grupos_produtos_niveis, on_update: :cascade, on_delete: :restrict }, index: { with: [:id], unique: true, :name => 'unique_grupos_produtos_id_nivel' }

      t.timestamps
    end
  end
end

它基本上创建了一个带有整数列的表,该列引用(foreign_key)另一个表并向索引添加一些选项。

现在,我需要对已经存在的表做同样的事情。我需要将这样的列添加到包含现有数据的表中。

我正在尝试:

class ChangeCadastrosPerfilNiveis < ActiveRecord::Migration
  def change
    add_column :cad_perfil_niveis, :perfil_niveis_nivel_id, :integer, :null => false, :default => 1, foreign_key: { references: :cad_perfil_niveis_niveis, on_update: :cascade, on_delete: :restrict }, index: { with: [:id], unique: true, :name => 'unique_perfil_niveis_id_nivel' }
  end
end

它不起作用。错误输出是:

-- add_column(:cad_perfil_niveis, :perfil_niveis_nivel_id, :integer, {:null=>false, :default=>1, :foreign_key=>{:references=>:cad_perfil_niveis_niveis, :on_update=>:cascade, :on_delete=>:restrict}, :index=>{:with=>[:id], :unique=>true, :name=>"unique_perfil_niveis_id_nivel"}})
rake aborted!
An error has occurred, this and all later migrations canceled:

wrong number of arguments (5 for 3)

回溯:

    wrong number of arguments (5 for 3)
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/foreigner-1.1.6/lib/foreigner/connection_adapters/sql2003.rb:17:in `add_foreign_key'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/schema_plus-1.1.2/lib/schema_plus/active_record/column_options_handler.rb:24:in `schema_plus_handle_column_options'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/schema_plus-1.1.2/lib/schema_plus/active_record/foreign_keys.rb:125:in `add_column'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/migration.rb:450:in `block in method_missing'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/migration.rb:424:in `block in say_with_time'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/benchmark.rb:280:in `measure'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/migration.rb:424:in `say_with_time'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/migration.rb:444:in `method_missing'
/home/sinetic/www/next/cadastros/db/migrate/20150223112512_change_cadastros_perfil_niveis.rb:4:in `change'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/migration.rb:393:in `block (2 levels) in migrate'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/benchmark.rb:280:in `measure'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/migration.rb:393:in `block in migrate'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:118:in `with_connection'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/migration.rb:377:in `migrate'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/migration.rb:512:in `migrate'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/migration.rb:704:in `block (2 levels) in migrate'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/migration.rb:759:in `call'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/migration.rb:759:in `block in ddl_transaction'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/transactions.rb:208:in `transaction'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/migration.rb:759:in `ddl_transaction'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/migration.rb:703:in `block in migrate'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/migration.rb:684:in `each'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/migration.rb:684:in `migrate'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/migration.rb:554:in `up'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/migration.rb:535:in `migrate'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.2/lib/active_record/railties/databases.rake:153:in `block (2 levels) in <top (required)>'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/rake/task.rb:205:in `call'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/rake/task.rb:205:in `block in execute'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/rake/task.rb:200:in `each'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/rake/task.rb:200:in `execute'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/rake/task.rb:158:in `block in invoke_with_call_chain'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/rake/task.rb:151:in `invoke_with_call_chain'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/rake/task.rb:144:in `invoke'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/rake/application.rb:116:in `invoke_task'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/rake/application.rb:94:in `block (2 levels) in top_level'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/rake/application.rb:94:in `each'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/rake/application.rb:94:in `block in top_level'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/rake/application.rb:88:in `top_level'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/rake/application.rb:66:in `block in run'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling'
/home/sinetic/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/rake/application.rb:63:in `run'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547@global/gems/rake-0.9.2.2/bin/rake:32:in `<top (required)>'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/bin/rake:23:in `load'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/bin/rake:23:in `<main>'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/bin/ruby_executable_hooks:15:in `eval'
/home/sinetic/.rvm/gems/ruby-1.9.3-p547/bin/ruby_executable_hooks:15:in `<main>'

如何将此选项添加到相关表中而不删除它并再次创建它?

1 个答案:

答案 0 :(得分:1)

尝试使用change_table方法:

change_table(:cad_perfil_niveis) do |t|
  t.integer :perfil_niveis_nivel_id, :null => false, :default => 1, foreign_key: { references: :cad_perfil_niveis_niveis, on_update: :cascade, on_delete: :restrict }, index: { with: [:id], unique: true, :name => 'unique_perfil_niveis_id_nivel' }
end