1

I want to change the string of the database to float for easy query,After the MySQL database is migrated and the code is submitted, no error occurs. Then pull the code in the postgres database and migrate it, but an error occurs

I have a migration, it's like this

class ChangeBalanceAndCnyBalanceToShiLianTongAssets < ActiveRecord::Migration[6.1]
  def up
    change_column :assets, :balance, :float, :decimal, precision: 32, scale: 7, null: false, default: 0
    change_column :assets, :cny_balance, :float, :decimal, precision: 32, scale: 7, null: false, default: 0
  end

  def down
    change_column :assets, :balance, :string
    change_column :assets, :cny_balance, :string
  end
end

But an error occurred during the operation:


== 20230104070823 ChangeBalanceAndCnyBalanceToShiLianTongAssets: migrating ====
-- change_column(:shi_lian_tong_assets, :balance, :float)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::DatatypeMismatch: ERROR:  column "balance" cannot be cast automatically to type double precision
HINT:  You might need to specify "USING balance::double precision".
/workspace/sneak_data/db/migrate/20230104070823_change_balance_and_cny_balance_to_shi_lian_tong_assets.rb:3:in `up'
/home/linlin/.asdf/installs/ruby/3.1.2/bin/bundle:25:in `load'
/home/linlin/.asdf/installs/ruby/3.1.2/bin/bundle:25:in `<main>'

I want to change the string to float for saving,How can I convert it to float and save the last seven decimal places of the original data,What should I do?

engineersmnky
  • 25,495
  • 2
  • 36
  • 52
zero20210602
  • 70
  • 1
  • 7

0 Answers0