1

I have table in rails calibration_sessions:

t.uuid "calibration_cycle_id", null: false
t.datetime "created_at", null: false
t.integer "creator_id", null: false
t.datetime "deleted_at"
t.string "name", null: false
t.jsonb "state_transition_progress", default: []
t.string "status", null: false
t.datetime "updated_at", null: false
t.integer "updater_id", null: false
t.uuid "uuid", default: -> { "uuid_generate_v4()" }, null: false
t.index ["calibration_cycle_id", "status", "deleted_at"], name: "index_calibration_sessions_on_cycle_and_status_and_deleted_at", using: :btree
t.index ["creator_id"], name: "index_calibrations_sessions_on_creator_id", using: :btree

the above is the schema for the table.

I am trying to change the type of column calibration_cycle_id to type INT.

TRUNCATE TABLE calibrations_sessions;
ALTER TABLE calibrations_sessions ALTER COLUMN calibration_cycle_id TYPE INT USING calibration_cycle_id::integer;

How ever I get the following error:

ERROR:  cannot cast type uuid to integer
LINE 1: ...ation_cycle_id TYPE INT USING calibration_cycle_id::integer;

What am I doing wrong here?

Mahesh Mesta
  • 793
  • 1
  • 11
  • 28

1 Answers1

1

UUID is a 32 digit type that cannot be automatically converted to a int data type which is too small.

You would need to write your own conversion function. See for example Create big integer from the big end of a uuid in PostgreSQL

pifor
  • 7,419
  • 2
  • 8
  • 16