fireproofsocks
I’m working on a table to store shipping addresses. I would like to add a fingerprint of each address so that I can more easily tell whether or not a given address has already been added. For simplicity, let’s assume that I’ve worked out a nice fingerprinting algorithm to normalize my addresses and identify them via a single string… let’s say this function is named simply fingerprint(address).
My question is about Ecto and changesets: how and where can I add this field to the database record when I save it? I’m unclear on how the cast and other functions are working inside the changeset function.
If I try to save a new address and its fingerprint matches an existing address, the save operation should fail.
If I try to edit an existing address and change it so it matches an existing address, the update operation should fail.
Any help on this would be appreciated… I’m having a hard time wrapping my head around where to add this and how to think about it in an idiomatic way. Thanks!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Most Liked- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
Probably something like this…
It is not tested, and fields might not correspond to your structure, but You can get the idea… the 2 last lines from changeset are important.
kokolegorille
This should not work… as it should be a field
You should use
In the example I gave, whatever is the shemas, and address is just a field of %Whatever{}
You might find ecto changeset structure here. From docs…
kokolegorille
You might show your schema, and migration file. Be sure :fingerprint field exists, as string…
You might also insert some IO.inspect in your pipeline to show the changeset, like so
The function fingerprint take a changeset, and if it is valid, it should persists the change.
If it is clearer, You could have used pattern matching, like so.
BTW Try to change the name of the function to generate_fingerprint(), just to be sure it does not conflict with the field name.
Last Post!
fireproofsocks
Thank you all! I got it working and I learned a lot in the process – inspecting the changeset with IO.inspect() was helpful because I could see the diffs and where fields were being included in or omitted from the changeset. I ran into a bit of trouble because I had set a default value for country, so that would cause that key to be missing from the changeset. Similarly, with the optional “street2” value, if it was sent with a blank value or omitted entirely, the changeset would end up without that field.