There is an application on ruby ​​on rails

schema.rb

ActiveRecord::Schema.define(version: 20170302072830) do create_table "stocks", force: :cascade do |t| t.string "name", null: false t.integer "unit_price", null: false t.integer "interest", null: false t.integer "duration", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "user_id" t.index ["user_id"], name: "index_stocks_on_user_id", using: :btree end create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "username", default: "", null: false t.datetime "remember_created_at" t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["email"], name: "index_users_on_email", unique: true, using: :btree end end 

There have been several migrations under development.

enter image description here

Migrating to Heroku heroku rake db: migrate

I get a log

enter image description here

And what about the duration field and its integer type? There was a type change from date to integer.

ALTER TABLE "stocks" ALTER COLUMN "duration" TYPE integer PG :: DatatypeMismatch: ERROR: column "duration"

Well, his clue does not tell me anything

HINT: You might need to specify "USING duration :: integer".

    2 answers 2

    Your type change can potentially lead to data loss . PostgreSQL implicitly makes only those transformations that do not result in losses, but you move to a more “narrow” type, so you have to explicitly specify how to convert existing values. This is such a precaution so that you do not kill your own data with a light wave.

    Postgres even suggested to you how to correct the command for changing the column at the level of the postgres language, all you had to do was translate this tip into rail syntax:

     change_column :stocks, :duration, "integer USING duration::integer" # явное преобразование ^^^^^^^^^^^^^^^^^^^^^^^ 

      Probably due to the fact that in different migrations the same fields I had had a different type of data, this error occurred.

      It would be correct for new migrations not to change the field type, but to delete it and create a new one.

      In my case, I completely deleted the old migrations, created a new one where I specified all the tables with already valid data and this solved the problem.

      • It would be correct for new migrations not to change the field type, but to delete it and create a new one. - no, wrong. It was possible to change and existing without losing data. - D-side
      • Okay, losing, but those that you, judging by the types, are not needed. Described what was right to do in the next answer. - D-side
      • Perhaps, but at that time I did not know whether it was possible to change old migrations, whether it would affect how the application works. Now I know several ways) - Sergei R
      • Migrations cannot be changed only by those that have already been completed on important bases. You have an important base, as I understand it, only on a hero :) - D-side
      • I have a training project and the base is not important where) - Sergei R