pop icon indicating copy to clipboard operation
pop copied to clipboard

Eager Update.

Open Nemesisesq opened this issue 6 years ago • 11 comments

If the parent already exists the children cannot be eagerly updated.

Nemesisesq avatar Jun 21 '18 20:06 Nemesisesq

Eager Destroy would be fine as well.

autinerd avatar Oct 12 '18 17:10 autinerd

@stanislas-m

Here's the spec requested in #318

  • If the association is has-one, eagerly updating the parent model will update the child using fk_id (not considering id of the child in the given parent to make sure there's only one child). If the child doesn't exist in the database, it will create one using the given model.
  • If the association is has-many, eagerly updating the parent model will save every child and delete children in the database that don't exist in the given parent.
  • If the association is many-to-many, eagerly updating the parent model will link new children and unlink absent children in the link table and save every child.
  • The identity of children will be determined by their ids.
  • Eager update will eager update nested models in multiple levels.
  • Flat updating the model will link new children and unlink absent children in the link table if the association is many-to-many.

If there's something to change, let me know.

sunho avatar Mar 11 '19 01:03 sunho

@sunho I didn't forget about this, I'm just laking of time right now. I'll hope to find some time this month.

stanislas-m avatar Apr 04 '19 20:04 stanislas-m

@stanislas-m

Here's the spec requested in #318

* If the association is has-one, eagerly updating the parent model will update the child using fk_id (not considering id of the child in the given parent to make sure there's only one child). If the child doesn't exist in the database, it will create one using the given model.

👍

* If the association is has-many, eagerly updating the parent model will save every child and delete children in the database that don't exist in the given parent.

👍

* If the association is many-to-many, eagerly updating the parent model will link new children and unlink absent children in the link table. It will not change the children in the database.

And create children that doesn't already exist?

* The identity of children will be determined by their ids.

👍

* Eagerly updating a parent will eagerly update the children if the association is not many-to-many.

Is this referring to multiple levels of nesting in the update or?

If there's something to change, let me know.

This is pretty close to what we had in mind for the "Eager" update but what about the "Flat" update? What do you propose is done about nested object/association updates when there is no explicit "Eager"?

Thanks so much for writing this out! Looking good so far!

mclark4386 avatar Apr 05 '19 05:04 mclark4386

@mclark4386

  1. I think it would be useful to cause an error if the children don't exist in a many-to-many relationship. Because it is usually used to describe the relationship between the models that already exist.

  2. Yes, I was talking about multiple levels of nesting. Like category > article > comment.

  3. I think flat update should not care about the association since flat loading doesn't care about it.

sunho avatar Apr 06 '19 02:04 sunho

  1. Interesting, you can't think of a case it would be useful? (the books test in the pop association tests comes to mind)
  2. Gotcha.
  3. Flat Create does create associations, but not nested objects.

mclark4386 avatar Apr 08 '19 05:04 mclark4386

Oh I just read the code and finally got what you've been talking about! Then it would be reasonable to save children in many-to-many if it's an eager connection. I edited the earlier post, please take a look.

And I think there should be a way to disable flat update and create, because it can be unwanted for some people. I personally think making the default connection not to do flat update and create is idiomatic, but that would break the api. How about supporting rw tag for associations?

sunho avatar Apr 08 '19 12:04 sunho

@stanislas-m

Here's the spec requested in #318

* If the association is has-one, eagerly updating the parent model will update the child using fk_id (not considering id of the child in the given parent to make sure there's only one child). If the child doesn't exist in the database, it will create one using the given model.

* If the association is has-many, eagerly updating the parent model will save every child and delete children in the database that don't exist in the given parent.

* If the association is many-to-many, eagerly updating the parent model will link new children and unlink absent children in the link table and save every child.

* The identity of children will be determined by their ids.

* Eager update will eager update nested models in multiple levels.

* Flat updating the model will link new children and unlink absent children in the link table if the association is many-to-many.

If there's something to change, let me know.

@stanislas-m this looks good to me, would you mind giving a once over when you get a chance?

mclark4386 avatar Apr 11 '19 06:04 mclark4386

We've just hit this case too. Happy to help wherever/however we can!

aeneasr avatar Nov 27 '19 12:11 aeneasr

So... any updates here?

griffinmartin avatar Apr 09 '24 22:04 griffinmartin

I've completely forgotten about this. I don't really have time to work on this nowadays. Please feel free to take over if anyone is interested in this feature.

sunho avatar Apr 11 '24 02:04 sunho