PG::DatatypeMismatch:错误:列“状态”无法自动转换为整数类型

roc*_*ock 4 ruby-on-rails

我在开发中使用了 sqlite3,但在 PostgreSQL 迁移时遇到错误

在数据库的sqlite迁移中没有发现这样的错误。

错误日志

PG::DatatypeMismatch: ERROR:  column "status" cannot be cast automatically to type integer

HINT:  You might need to specify "USING status::integer".
/home/abc/abc/db/migrate/20200211044313_change_status_type_in_stages.rb:3:in `change'

Run Code Online (Sandbox Code Playgroud)

20200113224031_create_stages.rb

class CreateStages < ActiveRecord::Migration[5.2]
  def change
    create_table :stages do |t|
      t.string :stage
      t.boolean :status

      t.timestamps
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

db/20200211044313_change_status_type_in_stages.rb

  def change
    change_column :stages, :status, :integer
  end
Run Code Online (Sandbox Code Playgroud)

Sam*_*dhe 10

您需要指定如何转换数据。

  change_column :stages, :status, 'integer USING CAST(status AS integer)'
Run Code Online (Sandbox Code Playgroud)

或者

  change_column :stages, :status, :integer, using: 'status::integer'
Run Code Online (Sandbox Code Playgroud)

更改列类型 - 错误