activerecord-postgres_enum icon indicating copy to clipboard operation
activerecord-postgres_enum copied to clipboard

Adding array enum column in same migration as enum creation fails with "TypeError: can't quote Array"

Open anaulin opened this issue 3 years ago • 7 comments

Describe the bug The following migration:

def change
  create_enum :product_type, %w[one-off subscription]

  add_column :products, :types, :product_type, array: true, default: []
end

Fails with the error:

TypeError: can't quote Array

However, separating the create_enum into its own migration, running it, and then separately running a migration with add_column succeeds. (Running the two migrations together also fails.)

I did some exploration, and it seems the problem is that Rails doesn't know the type of the :types column when it is trying to serialize the default value for that column. If I rewrite the above migration as:

  create_enum :product_type, %w[one-off subscription]

  add_column :products, :types, :product_type, array: true

  Product.reset_column_information
  change_column_default :products, :types, default: []

This fails with the errors:

unknown OID 697877: failed to recognize type of 'types'. It will be treated as String.
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

can't quote Hash

Expected behavior I would expect this migration to work without having to first fully apply the create_enum line.

Context (please complete the following information):

  • OS: Debian GNU/Linux 11 (bullseye)
  • Rails 6.1.6.1
  • activerecord-postgres_enum (2.0.1)
  • postgres:13.5

anaulin avatar Jul 14 '22 02:07 anaulin

Thank you for the report!

bibendi avatar Jul 14 '22 06:07 bibendi

(Added some more details to description, from my own debugging.)

anaulin avatar Jul 18 '22 21:07 anaulin