Can I have a key in an Ecto schema that is not in my database table?

So I have a field called computed_link that I would like to conveniently have available in the Ecto schema that I pass around for a certain data structure that I have, but I don’t want this field stored in my database table. I’d just like to calculate it on the fly, then store it along with all the other info in the struct that I pass to templates and whatnot. I tried doing this and got this error:

column q0.computed_link does not exist

Is there some way I can make this work? Some hack?

The only other way I have of doing this is converting all the data into a map and then adding this field. It seems like it makes more sense just to declare that the field should be available.

You can use

field :computed_link, ... virtual: true

https://hexdocs.pm/ecto/Ecto.Schema.html#field/3

2 Likes

Man, what a great community this is. Apologies for not RTFMing!

1 Like