-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
CreatedAt set to current date if empty when Update #110
Comments
I think this work around should work: repo.Insert(ctx, record, rel.NewStructset(record, false), rel.Set("created_at", nil)) We need to analyze how many change needed if we introduce ops type to mutator, especially since inserting and updating association is actually treated as save (update if id non zero, else insert) right now, which is a more general ops type. Another solution is to have a separate timestamp mutator like discussed #81 (https://github.com/Fs02/rel/projects/1#card-42354033) |
Why not use the same concept here, if |
Ah you are right, we should be able to do that 🎉 |
@Fs02 pls assign me |
Sure, let me know if you need any help 😄 To recap, the agreed solution is to check whether primary field is zero before assigning created at for Structset. Line 31 in 1d1e890
|
@Fs02 I think that we should actually decide this based on the op type as you mentioned at some point. Otherwise there will be no default value assigned for created_at for inserts with id. Not sure how bad this actually would be because db itself still should set the default value. |
Isn't it the same? I think in both cases, created_at still doesn't have default value and need to be handled in db (either by allowing null for the time type, or by setting using default argument). |
After looking more to the problem, I think you are right, I think we can't depends on ID to decide when to set created_at or not. there's a case when we want to create a record with custom predefined ID, but still want created_at to be set. |
@Fs02 It doesn't make to do this now especially when there are additional problems because of this trade off. We should create an issue for this an close this one. |
@KMPolakowski I'm unassigning you for now since the solution for these issue is not very clear yet at the moment, if you have proposal, I'll be more than glad to hear 😄 I'll let this open for now until a good/concrete solution is found unless @mhewedy decided to close this. |
@Fs02 To find out the ops type we simply have to query db if record with given id exists then when it exists treat as update else insert. When no primary key supplied then treat as insert. The question is where to do this query and how. |
@KMPolakowski if we add a query to find out the ops type, then I think this will be expensive, now all insert and update operation will have one additional query. Also, mutator are not supposed to access database, so we need to give the ops type before db is accessed:
Save is the most ambiguous op right now, and it's used internally when saving association. |
@Fs02 For Save OP we could introduce a isNew param to the entities which will tell whether the Document has been fetched from the db, indicating update or has been created, indicating insert. Yet we can't do this because we have model structs defined by the user. Well I don't think there're other options except querying or assuming that primary key means update as it was propsed in the first place. I opt for the latter. |
@KMPolakowski agree, for saving association, the mostt feasible way right now is to assume based on primary key. |
@Fs02 you can assign me so I can continue |
@KMPolakowski Sure, Thanks! 🎉 |
@Fs02 t := time.Parse(...)
lve.CreatedAt = t
lve.UpdatedAt = t // will be ignored
err = repo.Insert(ctx, &lve) |
yes, for now the workaround is: repo.Insert(ctx, record, rel.NewStructset(record, false), rel.Set("updated_at", t)) maybe a better improvement from REL side is to add a two new mutator: |
t := time.Parse(...)
changeset := rel.NewChangeset(lvp)
lvp.UpdatedAt = t // will be ignored
err = repo.Update(ctx, lvp, changeset) how changeset convert to NewStructset err = repo.Update(ctx, lvp, changeset, rel.NewStructset(changeset, false), rel.Set("updated_at", t)) ? |
you can't really convert changeset to structset, but you should be able to just do this:
|
thanks |
Found:
e2.CreatedAt gets updated:
Expected:
e2.CreatedAt should kept unchanged (
2020-09-24 23:43:53 +0300
)full source code: https://gist.github.com/mhewedy/a90cd7946907e9aabd04c873cb326c34
Suggestion:
I would suggest to check for the statement type here:
https://github.com/Fs02/rel/blob/c7b126f33cb90e773956ad66ee10a4fa5cdc6660/structset.go#L31
If it is insert then execute the
CreatedAt
logic, otherwise skip.What do you think?
The text was updated successfully, but these errors were encountered: