My rails app imports a bunch of resources from an third party service, that is the app source of truth. The API answers with a json resource and its id, for instance :
{"_id"=>"604d3d6ed64d52b8bfe84b",
"name"=>"Advanced Stock Trading Course + Strategies",
"author"=>"davd@fullsts.com",
}
My app import those resources and reproduces them in their own tables in a postgres db. Something that looks like :
create_table "programs", force: :cascade do |t|
t.jsonb "lms_data", default: "{}", null: false
t.bigint "author_id"
t.jsonb "default_lms_image", default: "{}", null: false
t.index ["account_id"], name: "index_programs_on_account_id"
t.index ["author_id"], name: "index_programs_on_author_id"
t.index ["lms_data"], name: "index_programs_on_lms_data", using: :gin
end
where lms_data stores the original json from the third party API.
Originally, I created this table with a classic Bigint ID primary key in auto-increment mode. As the third party service is my only source of truth. I would actually like their id to become my primary key "_id"=>"604d3d6ed64d52b8bfe84b", that is the string "604d3d6ed64d52b8bfe84b".
However I have other tables that already reference program_id as a foreign key and when I try to run this migration for instance :
def change
change_column :sessions, :program_id, :string
change_column :tickets, :program_id, :string
change_column :programs, :id, :string
end
def down
change_column :sessions, :program_id, :bigint
change_column :tickets, :program_id, :bigint
change_column :programs, :id, :bigint
end
I am getting this error :
Caused by:
ActiveRecord::StatementInvalid: PG::DatatypeMismatch: ERROR: foreign key constraint "fk_rails_35ef875d99" cannot be implemented
DETAIL: Key columns "program_id" and "id" are of incompatible types: character varying and bigint.
Is there a way to migrate my primary and foreign keys in this situation ?