-
-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Polymorphic embeds are not kept as changesets but are turned into structs - as opposed to what happens with ordinary embeds #74
Comments
This was due to a limitation with what I can do with Ecto Type, if I recall correctly. Does this behaviour cause you any issue? |
Yes. When I call Ecto.Changeset APIs like I added a workaround in my code, but I thought you might want to know about this. It should be fixed IMO. I can give it a try and come up with a PR unless you tell me you prefer to keep it this way. |
As far as I can recall there was no possible fix, but if you you think you can find a way to make that work it'd be awesome. The idea of the project is to have the same behavior as embeds_one and embeds_many, so this is ideally fixed. |
Ok I've taken a look and I think I understand why you're doing this. You're applying the changes as soon as the polymorphic embedded changeset is valid here (in the case of single embeds) because otherwise, if you keep it as a changeset, Ecto would never apply the changes. That's because in Ecto.Changeset.apply_changes/1, Ecto treats embeds and associations as special cases where changes are recursively applied. In all other cases, the change is simply copied over to the data. Is this accurate? |
This could be important towards change tracking with Live View IIRC? If we replace with structs on every pass through the cast function, everything will be marked as a new change and sent over the wire for each run through phx_change. If it is impossible to fix, maybe we could float this to Ecto and see if there is anything to do on that end? |
I think this behaviour also leads to another bug I'm encountering: If you have a "normal" If you think this is unrelated i can open up another issue! |
Observed behavior: When I build a changeset with ordinary embeds, these embeds stay changesets. When I build the changesets with polymorphic embeds, the embeds are turned into structs if they're valid.
Expected behavior: I would expect polymorphic embeds to be kept as changeset until I call
apply_changes
orapply_action
on the parent changeset, just as normal embeds do.Ecto version: 3.9.4
polymorphic_embed version: 3.0.5
Here's a working example:
Result of executing the above code:
changeset for schema with traditional embeds: embeds are still changesets
changeset for schema with polymorphic embeds: changes in embeds have already been applied
The text was updated successfully, but these errors were encountered: