I've got a legacy database that I'm trying to pull into Ecto. In it there's an orders
table which has an order_status_id
column. order_status_id
maps to a set of constants in the legacy system.
I'd like to have the MyApp.Order
struct contain an order_status
field, which has a custom type that converts the integer IDs to meaningful atoms. I've got the custom type working, but I can't figure out how to map a field named order_status
to a column named order_status_id
.
The legacy system is still online and using the database, so changing the DB schema is not an option. Is there any way to get this working?
I'm not sure if it was possible when the question was first asked, but it is possible now (Elixir 1.5). See https://hexdocs.pm/ecto/Ecto.Schema.html In particular the
@field_source_mapper
and:source
options. The:source
option is pretty easy -- just include it in yourschema
definition something like this:In the above example, the existing database table has columns named "legacy_foo_db_column" and "order_status", but internally in the Elixir app, the schema and changeset etc. will use attributes named "foo" and "status"
I think, it is currently not possible to simply associate model field with different column name.
If you really want to use name
order_status
, you can create additional virtual field. Those fields are not persisted to database. Then yourcast
function should be responsible for changingorder_status_id
based on params.In theory, you can also use changeset function to do the opposite operation: set
order_status
based onorder_status_id
, but it adds complexity.In my opinion, the best solution would be to simply accept
order_status_id
name.