Fix: PG::UndefinedObject: ERROR: type "integer_array" does not exist
Rails migrations can sometimes be a migraine. Yesterday while upgrading a Ruby on Rails application I noticed an unusual error. It was related to a migration that created an integer array in a PostgreSQL table.
class AddFundIdsColumnToTransactions < ActiveRecord::Migration
def change
add_column :transactions, :fund_ids, :integer_array, default: '{}'
end
end
This migration resulted in an error when running rails db:setup
(or rails db:migrate
).
== 20160925181242 AddFundIdsColumnToTransactions: migrating ===================
-- add_column(:transactions, :fund_ids, :integer_array, {:default=>"{}"})
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedObject: ERROR: type "integer_array" does not exist
LINE 1: SELECT 'integer_array'::regtype::oid
^
: SELECT 'integer_array'::regtype::oid
active_record/connection_adapters/postgresql/database_statements.rb:98:in `async_exec'
active_record/connection_adapters/postgresql/database_statements.rb:98:in `block in execute'
This error came as a surprise to me. On the one hand, I couldn't find a single post on Google regarding the error. On the other hand, despite being familiar with PostgresSQL integer arrays, I had no recollection of using a migration like above.
How ever once I updated the migration code to the familiar syntax, the migrations proceeded without incident.
class AddFundIdsColumnToTransactions < ActiveRecord::Migration
def change
add_column :transactions, :fund_ids, :integer, array: true, default: []
end
end
About the Author
Ziyan Junaideen -
Ziyan is an expert Ruby on Rails web developer with 8 years of experience specializing in SaaS applications. He spends his free time he writes blogs, drawing on his iPad, shoots photos.