schnittchen
I have a table with a generated column (PostgreSQL: Documentation: 18: 5.4. Generated Columns).
So far I only used it inside queries using a fragment, which was sufficient since I never needed to read the value from a loaded schema, so I simply ommitted the field declaration for it.
Now that I want to load the value, I run into the problem that even when I only change a different field on an existing record, Ecto seems to generate an update for the value in the column, which Postgres correcly rejects with an error. What I would need is kind of the opposite of the load_in_query flag, to prevent that and treat the column as immutable (which would introduce the problem that I’d get back a stale value after an update, but that’s not my concern right now).
I searched but couldn’t find anything related, and it looks like I need to work around the problem for the time being.
Does anyone else have experience with exposing a generated column on an Ecto Schema?
Trending in Discussions
Other Trending Topics
Chat & Discussions>Discussions
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex











First 4 of 4 Posts
fuelen
Try to add
read_after_writes: trueoption to the field. Maybe I test it incorrectly, but it works fine for mefuelen
Table
Insert sample data:
Schema:
Test:
D4no0
You can simply use the option
virtual: true, but beware that no actual checks will be done for that field, meaning that declaring a field is just to make sure that you will not get complains from ecto when selecting into a schema, for example:will work, however you can place any value in that field and ecto will never complain, that includes custom types, they will never even try to validate or transform the received result.
schnittchen
Thanks, and sorry for the late reply. This is what it looks like for me, in the schema:
It’s been like this for a few weeks now and it works quite well.
I also wrote
contains_day?andoverlaps_date_range?macros to make using thedate_rangeeasier when querying. Those are far from perfect though.