-
Notifications
You must be signed in to change notification settings - Fork 125
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
[Feature] SQL: Add all combinations of element refs and ids for 'create' methods #586
Comments
PR which implements this: #576 |
Hi @EotT123. Thanks for the PR. My only reservation is the exponential growth of generated functions with FKs. Four or more FKs feels like too many. This is why I chose to have either all refs or all IDs. And use the all ID version when you don't have all the required FK refs. Calling getId() isn't so bad? Perhaps I'll make this a configurable option... Support a boolean What I really want is a union type: create(Foo | Integer foo, Bar | Integer bar, ...) But that's probably a bridge too far, for now. |
Thanks for your answer. I was also thinking about the exponential growth, and the fact that calling
In this example, calling The union type is a nice idea, but that's a complete new feature? And how would such a type be handled, because you don't know what the type is? |
That can be made to work... In fact the information is there, it's just not made available until after the commit. Basically, after a create or update any generated data in the record is automatically retrieved and placed "on hold" in a private data section until the commit succeeds or fails. If it succeeds, the on-hold data becomes active, otherwise it is discarded. I am considering delegating to on-hold/generated data during the commit. It's a straightforward change that probably should be done regardless of the outcome of this issue.
Yep, it would be a new feature. The type can be identified with simple In any case I do like your PR, but I think it should be optional via dbconfig settings. I'll merge it and add the option and get a feel for it. I'll also probably experiment with making the on-hold data indirectly accessible during the commit. |
That might be a better solution. Then there even might not be the need to pass the objects by reference, only by id? I'm a bit intrigued how you managed to do this. Autogenerated id's are generated by the database, when persisting the object. How do you already have access to the id's, before the object is persisted? |
Well, there's no free lunch, but the price is low. Here's an example illustrating what I mean by making generated data available during the commit. Foo foo = Foo.create(...);
MyDatabase.addSqlChange(ctx -> Bar.create(foo.getId(), ... ));
. . .
MyDatabase.commit(); During a commit The MyDatabase.commit(ctx -> {
Foo foo = Foo.create(...);
Bar.create(foo.getId(), ... );
. . .
} ); With eager execution instead of batching INSERT ops we execute them with Still not a free lunch as it must execute in a Probably more than you wanted to know, but that's the gist of it. |
Maybe another way to understand this, your example involving refs works by virtue of the "on hold" bindings. Foo foo = Foo.create(...);
Bar bar = Bar.create(foo, ...);
MyDB.commit(); Here the Ok. Time for a weekend! |
Thanks for the explanation. I was thinking about something like it, but I was wondering if I maybe was missing something. All seems logical now. Thanks. Probably the following would work too (although this is also not an optimal solution, and it doesn't solve anything, as we need separate methods for this to, same as what the PR adds.):
In this example, getting the id is function, which is lazily executed, when the id is available. Have a nice weekend! |
Enhance the
create
methods for SQL objects by offering more flexibility for handling foreign keys. Currently, users have to choose between object references or IDs, but not a mix of them.Example:
The text was updated successfully, but these errors were encountered: