Skip to content

Mongodb work around causes issues with ecto enums as type field #141

@jasonlatta

Description

@jasonlatta

I was attempting to use an EnumType as the data type for my polymorphic type field. However, due to the following line I encountered issues with how it dumped the field into the db:

# use the atom instead of string form for mongodb
|> Map.put(type_field_name, do_get_polymorphic_type(module, types_metadata))

Instead of getting the enum's string value as expected, I got the fully qualified enum module name. I can just accept the different DB value, but it isn't ideal, and this overwriting of how the field encodes itself seems like it could pose other issues for users of the library.
A better fix for mongo db would seem to be letting the user define their own type for the field that dumps it the way they wish.

For reference, here is my ecto schema definition for the polymorphic embed:

  schema "" do
    ....
    polymorphic_embeds_one(:tls,
      types: TlsContext.polymorphic_embeds_types(),
      on_replace: :update,
      type_field_name: :format
    )
    ...
  end

defmodule TlsContext do
  def polymorphic_embeds_types do
    [
      TlsFormat.JKS => JKS_Module
      TlsFormat.PEM => PEM_Module
    ]
  end
end

and the enum definition:

  defenum TlsFormat, :string do
    value JKS, "JKS"
    value PEM, "PEM"
  end

The polymorphic modules define the format field as being type TlsFormat.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions