-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serializer failing on schemas with a composite type, i.e. Money.Ecto.Composite.Type
#230
Comments
@kipcole9 (Kip, just CCing you so you are aware of this issue) |
See [papertrail issue](izelnakri/paper_trail#230). Papertrail calls `Ecto.embedded_dump/2` for all schemas, not just embedded schemas. This commit detects if `dump/2` is called from `Ecto.embedded_dump/2` and if so returns a map instead of the usual tuple.
Thanks for the ping @frahugo. This is a bit tricky since it's not possible to round-trip json encoding of a schema. But thats ok in this case since papertrail's use case doesn't include decoding it. Possible resolutionI have pushed a commit that will detect if Please try the papertrail branch by configuring: {:ex_money_sql, github: "kipcole9/money_sql", branch: "papertrail"} In It's a bit hacky, but it's all public APIs. There will only be an issue if Ecto somehow changes its public API - and thats not very likely. Please open an issue on the money_sql repo if you see any problems. If it tests cleanly then I'll publish a new version. Note this is not a general purpose solution to the problem. Not all Postgres types can be serialised to JSON (without a custom encoder) and therefore it should be expected that other issues arise from time-to-time. SummaryThe approach you are taking in your snippet is the same as what the Postgres ROW_TO_JSON does so I think its the right approach, given that there is no JSON serialisation format for a tuple. The tricky bit, as you point out, is that One other constraint is that the Money.Ecto.Composite.Type is configured with |
@kipcole9 Your proposed solution works great. If you generate a new version of ex_money, we'll use it ;-) Thanks a lot |
@frahugo I have published ex_money_sql version 1.11.0 with the following changelog entry: Enhancements
Note this enhancement is a workaround for papertrail - it does not fix the underlying issue which is that not all database types can be serialised to JSON. I will send a documentation PR to the papertrail repo. |
See pull request 231. |
Reporting an issue with the way the serialization is performed in version 1.0.0.
Our application uses
ex_money_sql
for a money composite type in some of our Ecto schema. For instance:According to
ex_money_sql
documentation, if we were to use the money field in an embedded schema, we would use another type:We are now getting this error when we use
PaperTrail.insert
with anAccount
changeset, i.e.As we can see, Jason tries to encode a tuple that was meant to be sent to Postgresql (composite type).
This is caused by the use of
Ecto.embedded_dump/2
inPaperTrail.Serializer
.According to the Ecto documentation, the
embedded_dump
function should be given an embedded schema:As a workaround, we had to implement a hack:
So is there another way to generate the json data to be stored in
Version
?The text was updated successfully, but these errors were encountered: