-
Notifications
You must be signed in to change notification settings - Fork 26
Open
Labels
docsDocs are incorrect or incompleteDocs are incorrect or incomplete
Description
When trying to use ActiveRecord's Enum stuff with an enum column I end up with a type mismatch when creating records. ActiveRecord defaults to converting the enum values to integers which does not map correctly to PostgreSQL's enum types. The workaround is to specify each value as the string version of the enum.
Model
class AddOn < ActiveRecord::Base
enum category: %i[thermostat humidifier cleaner purifier]
end
AddOn.create(category: "thermostat")Error
(0.1ms) BEGIN
AddOn Create (0.4ms) INSERT INTO "add_ons" ("created_at", "updated_at", "category") VALUES ($1, $2, $3) RETURNING "id" [["created_at", "2019-03-05 16:35:17.980836"], ["updated_at", "2019-03-05 16:35:17.980836"], ["category", 0]]
(0.0ms) ROLLBACK
Traceback (most recent call last):
1: from (irb):1
ActiveRecord::StatementInvalid (PG::InvalidTextRepresentation: ERROR: invalid input value for enum add_on_categories: "0")
: INSERT INTO "add_ons" ("created_at", "updated_at", "category") VALUES ($1, $2, $3) RETURNING "id"
Workaround
class AddOn < ActiveRecord::Base
enum category: {
thermostat: "thermostat",
humidifier: "humidifier",
cleaner: "cleaner",
purifier: "purifier",
}
endMetadata
Metadata
Assignees
Labels
docsDocs are incorrect or incompleteDocs are incorrect or incomplete