在ROR中将整数更改为bigint

时间:2015-03-15 14:01:29

标签: ruby-on-rails activerecord command-line migration heroku-postgres

我需要将表格整数更改为BIGINT 我正在使用RoRSQLLite

如何通过命令行将IntBigInt的现有表字段修改?

1 个答案:

答案 0 :(得分:2)

您可以通过迁移执行此操作。以下是文档的链接:http://edgeguides.rubyonrails.org/active_record_migrations.html

基本上,你需要运行:

rails g migration change_your_column_to_bigint

并将此代码添加到迁移新迁移文件中:

def up
  execute <<-SQL
    ALTER TABLE ip_to_countries
    ALTER COLUMN your_column TYPE bigint USING your_column::bigint
  SQL
end

为了记录,我从这里偷走了这个:How do I change a string column into a bigint?,但没有将其标记为重复,因为它涉及转换字符串列。

现在我收到此错误

SQLite3::SQLException: near "ALTER": syntax error:     ALTER TABLE posts
    ALTER COLUMN mileage TYPE bigint USING mileage::bigint
C:/Sites/jeepjig/db/migrate/20150315145856_change_mileage_to_bigint.rb:3:in `change'
C:in `migrate'
ActiveRecord::StatementInvalid: SQLite3::SQLException: near "ALTER": syntax error:     ALTER TABL
    ALTER COLUMN mileage TYPE bigint USING mileage::bigint
C:/Sites/jeepjig/db/migrate/20150315145856_change_mileage_to_bigint.rb:3:in `change'
C:in `migrate'
SQLite3::SQLException: near "ALTER": syntax error
C:/Sites/jeepjig/db/migrate/20150315145856_change_mileage_to_bigint.rb:3:in `change'
C:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
运行此

class ChangeMileageToBigint < ActiveRecord::Migration
  def change
  execute <<-SQL
    ALTER TABLE posts
    ALTER COLUMN mileage TYPE bigint USING mileage::bigint
  SQL
end
end