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
I wasn't able to get timestamps support in embedded_schema. In result timestamp fields are nil for all embedded documents but it works fine for regular documents. Do i miss something, is there a way to get timestamps support for embedded_schema?
I realize this issue is really old, but it is still an issue which I assume is why it's still open. Since mongoldb_ecto 1.0 is in beta, I assume this is a bug that would need to be fixed before the final release.
When using the timestamps macro from Ecto.Schema in an embedded schema, the inserted_at and updated_at field values are always nil/null.
@bdtomlin This is a good question! I'm not quite sure how that would be implemented, but my guess is it wouldn't be straightforward conceptually or technically and possibly impossible. Timestamps can be set on the root document whenever it's created/updated, but embedded documents don't have quite as clean of a lifecycle. For instance, I could insert a bunch of comments into a post document and then create the post -- what timestamp do the comments get? Is it the timestamp of when they were added to the post struct or when the post was inserted into the DB? In terms of implementation, my guess is the implementation of timestamps on the root document is straight forward (e.g. add a $set on all the insert* or update*` functions) but how do you implement it on embedded documents that might be embedded deep in a tree?
I'm going to close this for now as I don't see a path forward for this feature. However, if anyone has any ideas on how to implement this, I would welcome the PR and would love to hear ideas on how to make it happen!
I will try to come back to this in the future, but I think it would help to leave it open so it still has visibility for me or anyone else who may need this feature and has some time to work on it.
Activity
bdtomlin commentedon Dec 6, 2021
I realize this issue is really old, but it is still an issue which I assume is why it's still open. Since mongoldb_ecto 1.0 is in beta, I assume this is a bug that would need to be fixed before the final release.
When using the
timestamps
macro from Ecto.Schema in an embedded schema, theinserted_at
andupdated_at
field values are always nil/null.scottmessinger commentedon Dec 8, 2021
@bdtomlin This is a good question! I'm not quite sure how that would be implemented, but my guess is it wouldn't be straightforward conceptually or technically and possibly impossible. Timestamps can be set on the root document whenever it's created/updated, but embedded documents don't have quite as clean of a lifecycle. For instance, I could insert a bunch of comments into a post document and then create the post -- what timestamp do the comments get? Is it the timestamp of when they were added to the post struct or when the post was inserted into the DB? In terms of implementation, my guess is the implementation of timestamps on the root document is straight forward (e.g. add a
$set
on all theinsert*
or update*` functions) but how do you implement it on embedded documents that might be embedded deep in a tree?I'm going to close this for now as I don't see a path forward for this feature. However, if anyone has any ideas on how to implement this, I would welcome the PR and would love to hear ideas on how to make it happen!
bdtomlin commentedon Dec 8, 2021
@scottmessinger,
Mongoid handles this with what it calls cascading callbacks. That name gives a pretty decent hint of a way to handle it. https://docs.mongodb.com/mongoid/current/tutorials/mongoid-relations/#cascading-callbacks
I will try to come back to this in the future, but I think it would help to leave it open so it still has visibility for me or anyone else who may need this feature and has some time to work on it.