mathieuprog / polymorphic_embed

Polymorphic embeds in Ecto
Apache License 2.0
325 stars 63 forks source link

Error occured when using with mongodb_ecto #82

Closed 5HARK closed 3 months ago

5HARK commented 1 year ago

When i using polymorphic_embed with mongodb_ecto on MongoDB, below error occured:

iex(19)> PosServiceAdmin.Stores.create_store(%{ status: "approved", store: %{ __type__: :local, name: "Candy store" } }) 
[debug] QUERY ERROR
COMMAND [insert: "stores", documents: [[_id: #BSON.ObjectId<645bc685da6776dc01f6bd85>, status: "approved", store: [{:name, "Candy store"}, {"__type__", :local}], inserted_at: ~U[2023-05-10 16:29:57Z], updated_at: ~U[2023-05-10 16:29:57Z]]], writeConcern: %{}] [[insert: "stores", documents: [[_id: #BSON.ObjectId<645bc685da6776dc01f6bd85>, status: "approved", store: [{:name, "Candy store"}, {"__type__", :local}], inserted_at: ~U[2023-05-10 16:29:57Z], updated_at: ~U[2023-05-10 16:29:57Z]]], writeConcern: %{}]]
** (ArgumentError) invalid document containing atom and string keys: [{:name, "Candy store"}, {"__type__", :local}]
    (mongodb 1.0.0) lib/bson/encoder.ex:171: BSON.Encoder.invalid_doc/1
    (elixir 1.14.0) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
    (mongodb 1.0.0) lib/bson/encoder.ex:127: BSON.Encoder.document/1
    (mongodb 1.0.0) lib/bson/encoder.ex:146: anonymous fn/3 in BSON.Encoder.document/1
    (elixir 1.14.0) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
    (mongodb 1.0.0) lib/bson/encoder.ex:127: BSON.Encoder.document/1 
    (mongodb 1.0.0) lib/bson/encoder.ex:146: anonymous fn/3 in BSON.Encoder.document/1
    (elixir 1.14.0) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
    (mongodb 1.0.0) lib/bson/encoder.ex:127: BSON.Encoder.document/1
    (mongodb 1.0.0) lib/bson/encoder.ex:146: anonymous fn/3 in BSON.Encoder.document/1
    (elixir 1.14.0) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
    (mongodb 1.0.0) lib/bson/encoder.ex:127: BSON.Encoder.document/1
    (elixir 1.14.0) lib/enum.ex:1658: Enum."-map/2-lists^map/1-0-"/2
    (db_connection 2.4.3) lib/db_connection.ex:1317: DBConnection.maybe_encode/4
    (db_connection 2.4.3) lib/db_connection.ex:695: DBConnection.execute/4
    (mongodb 1.0.0) lib/mongo.ex:633: Mongo.direct_command/3
    (mongodb 1.0.0) lib/mongo.ex:700: Mongo.insert_one/4
    (mongodb_ecto 1.0.0) lib/mongo_ecto/connection.ex:180: Mongo.Ecto.Connection.insert/3
    (ecto 3.9.5) lib/ecto/repo/schema.ex:756: Ecto.Repo.Schema.apply/4
    (ecto 3.9.5) lib/ecto/repo/schema.ex:369: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
5HARK commented 1 year ago

I edit code like this, then it works now

https://github.com/torderdev/polymorphic_embed/commit/5e5cc88dd1b8af670cd73b08c113c797e3f3fdc5

mathieuprog commented 3 months ago

Thank you, I added the code change. Will release soon.