You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Database and version (PostgreSQL 9.4, MongoDB 3.2, etc.): PostgreSQL 12.3
Ecto version (mix deps): 3.4
Database adapter and version (mix deps): postgrex: 0.15.2, ecto_sql: 3.4.5
Operating system: Mac OS 10.14.6 (Mojave)
Current behavior
Summary: Changesets created with put_assoc do not support writes when associations are an abstract table.
Context:
Across my application, I want to track state changes for many types of records, eg orders, line items, etc. I want to add a state event record associated with a parent record each time that parent is created or updated. And I want to be able to use the same function in both the create and udpate scenarios, in order to isolate logic and lessen the burden for future devs who may be adding state events to new models.
Implementation:
The state event model is a good candidate for an abstract table because it is a canonical polymorphic association.
Creating state events on line item create and update:
Because line items are created through nested forms, we manage the creation of the line item and its associations through cast_assoc. So while the Ecto docs advocate for creating child records from the abstract table like this: Repo.insert!(build_assoc(post, :comments)), I prefer not to create state-events in this one-off fashion and instead hook into the changeset for the line item.
I could leverage either put_assoc or cast_assoc to do this. put_assoc is perhaps preferred because we are not taking in state event information as external params. However, put_assoc does not play nicely with the abstract state events table.
put_assoc vs cast_assoc:
Both generate changesets that look identical, but Postgrex throws an error on trying to insert the record using the changeset generated by put_assoc.
(Note: the examples here are for an insert case, but the same happens for an update to an existing line item.)
Using put_assoc cannot handle the abstract schema:
Environment
postgrex
: 0.15.2,ecto_sql
: 3.4.5Current behavior
Summary: Changesets created with
put_assoc
do not support writes when associations are an abstract table.Context:
Across my application, I want to track state changes for many types of records, eg orders, line items, etc. I want to add a state event record associated with a parent record each time that parent is created or updated. And I want to be able to use the same function in both the create and udpate scenarios, in order to isolate logic and lessen the burden for future devs who may be adding state events to new models.
Implementation:
The state event model is a good candidate for an abstract table because it is a canonical polymorphic association.
Abstract Schema:
Associated to a line item parent:
With the corresponding concrete table:
Creating state events on line item create and update:
Because line items are created through nested forms, we manage the creation of the line item and its associations through
cast_assoc
. So while the Ecto docs advocate for creating child records from the abstract table like this:Repo.insert!(build_assoc(post, :comments))
, I prefer not to create state-events in this one-off fashion and instead hook into the changeset for the line item.I could leverage either
put_assoc
orcast_assoc
to do this.put_assoc
is perhaps preferred because we are not taking in state event information as external params. However,put_assoc
does not play nicely with the abstract state events table.put_assoc vs cast_assoc:
Both generate changesets that look identical, but Postgrex throws an error on trying to insert the record using the changeset generated by
put_assoc
.(Note: the examples here are for an insert case, but the same happens for an update to an existing line item.)
Using
put_assoc
cannot handle the abstract schema:but cast_assoc can handle the abstract table:
Calling Repo.insert!() with the
cast_assoc_changeset
successfully creates the line item and the associated state event.Expected behavior
Changesets created by
put_assoc
should be able to support writes when the association is an abstract table, in the same way thatcast_assoc
can.The text was updated successfully, but these errors were encountered: