在Rails中将列数据类型从布尔值更改为整数

时间:2017-04-18 20:51:39

标签: ruby-on-rails postgresql activerecord

我的一个数据库表(我正在使用postgres)中有一个名为:active的列,它当前是一个布尔值,默认为true。我想将此列的类型从布尔值更改为整数,以便我可以使用此项目的枚举。我已经检查了文档和一些堆栈溢出的答案,但还没有找到一个可靠的答案。如何使用迁移进行此更改?我正在使用Rails 5,如果这有所作为。谢谢!

2 个答案:

答案 0 :(得分:2)

您的迁移文件应如下所示

gems/ruby-2.3.1@global/gems/hamlit-2.8.1/lib/hamlit/attribute_builder.rb:2:in `require': incompatible library version - /home/alex/.rvm/gems/ruby-2.3.1@global/gems/hamlit-2.8.1/lib/hamlit/hamlit.so (LoadError)
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/hamlit-2.8.1/lib/hamlit/attribute_builder.rb:2:in `<top (required)>'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/hamlit-2.8.1/lib/hamlit/attribute_compiler.rb:2:in `require'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/hamlit-2.8.1/lib/hamlit/attribute_compiler.rb:2:in `<top (required)>'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/hamlit-2.8.1/lib/hamlit/compiler/tag_compiler.rb:3:in `require'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/hamlit-2.8.1/lib/hamlit/compiler/tag_compiler.rb:3:in `<top (required)>'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/hamlit-2.8.1/lib/hamlit/compiler.rb:7:in `require'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/hamlit-2.8.1/lib/hamlit/compiler.rb:7:in `<top (required)>'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/hamlit-2.8.1/lib/hamlit/engine.rb:4:in `require'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/hamlit-2.8.1/lib/hamlit/engine.rb:4:in `<top (required)>'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/hamlit-2.8.1/lib/hamlit.rb:2:in `require'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/hamlit-2.8.1/lib/hamlit.rb:2:in `<top (required)>'
from /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:77:in `require'
from /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:77:in `block (2 levels) in require'
from /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:72:in `each'
from /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:72:in `block in require'
from /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:61:in `each'
from /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:61:in `require'
from /usr/lib/ruby/vendor_ruby/bundler.rb:99:in `require'
from /usr/local/RubyWorkspace/produtos_adaptativos/config/application.rb:14:in `<top (required)>'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:78:in `require'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:78:in `block in server'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:75:in `tap'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:75:in `server'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
from /usr/local/RubyWorkspace/produtos_adaptativos/bin/rails:9:in `require'
from /usr/local/RubyWorkspace/produtos_adaptativos/bin/rails:9:in `<top (required)>'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/spring-2.0.0/lib/spring/client/rails.rb:28:in `load'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/spring-2.0.0/lib/spring/client/rails.rb:28:in `call'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/spring-2.0.0/lib/spring/client/command.rb:7:in `call'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/spring-2.0.0/lib/spring/client.rb:30:in `run'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/spring-2.0.0/bin/spring:49:in `<top (required)>'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/spring-2.0.0/lib/spring/binstub.rb:31:in `load'
from /home/alex/.rvm/gems/ruby-2.3.1@global/gems/spring-2.0.0/lib/spring/binstub.rb:31:in `<top (required)>'
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:68:in `require'
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:68:in `require'
from /usr/local/RubyWorkspace/produtos_adaptativos/bin/spring:13:in `<top (required)>'
from bin/rails:3:in `load'
from bin/rails:3:in `<main>'

此外,您需要将所有以前的记录重写为整数值,这可以在同一个迁移文件中完成。

答案 1 :(得分:1)

您可以使用以下命令创建新迁移:

rails g migration change_data_type_for_active

然后编辑迁移以使用change_column

class ChangeDataTypeForActive < ActiveRecord::Migration

  def self.up
    change_column :table_name, :active, :integer
  end

  def self.down
    change_column :table_name, :active, :boolean
  end

end

然后运行迁移:

rake db:migrate

定义updown方法的原因是您可以使用以下命令将数据库返回到previos状态:

rake db:rollback