Hi,
I think, create-instance should defer the creation of an id (from sequence-next), because, there are some times when an object is created, but is not saved. For example, in a web application, I would need to create an object so that I can fill in the slots from the form values, but I might not save it, as it might have some validation errors.
It would save some sequence ids (a lot actually, as there might be multiple rounds of validation errors) if I don't have to assign an id unless it is being saved to the database. I think we could have insert-dao/save-dao to retrieve a new sequence id if the column "id" is not filled/nil/zero, and use an existing id if it's value is greater than zero. If this sounds ok, I can create a patch.
Regards, Vamsee.
Hello Vamsee,
You make a good point. However, sometimes it is useful to have the id of your new object before saving it. The next-id function can be used for that, of course. But deferring the creation of ids for all DAO objects might break some existing code. How moving the initialization of the auto-id field to an initialize-instance method, which only does it if no :defer-id (or something similar) keyword arg is given? Then, save/insert-dao could check whether the id field has a value, and initialize it when it does not.
Regards, Marijn
Marijn Haverbeke wrote:
How moving the initialization of the auto-id field to an initialize-instance method, which only does it if no :defer-id (or something similar) keyword arg is given? Then, save/insert-dao could check whether the id field has a value, and initialize it when it does not.
Sounds good to me. I've been trying to hack something into the set-fields method of deftable, haven't entirely succeeded yet, but your approach seems more elegant to me. I don't think I know the code well enough to do this design change, I'll be grateful if you can check in a solution, I'll test it out.
Thanks, Vamsee.
I have pushed a patch implementing the change I described. The deftable macro is slowly becoming a multi-headed monster, but hey, that only adds to its charm. Take a look and see whether this works for you.
Regards, Marijn
Marijn Haverbeke wrote:
I have pushed a patch implementing the change I described. The deftable macro is slowly becoming a multi-headed monster, but hey, that only adds to its charm. Take a look and see whether this works for you.
Thank you, works great - do you think the attached change to save-dao would be ok?
Regards, Vamsee.
Oh, I just realized your patch does not work when the auto-id slot is called something other than 'id', I pushed another patch to fix this.
Cheers, Marijn
postmodern-devel@common-lisp.net