fandyvon / activejdbc

Automatically exported from code.google.com/p/activejdbc
0 stars 0 forks source link

* Allow setting of ID on model and still run insert instead of update on save(); #6

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
* Allow setting of ID on model and still run insert instead of update on save();

Original issue reported on code.google.com by ipolevoy@gmail.com on 19 Aug 2010 at 4:45

GoogleCodeExporter commented 9 years ago

Original comment by ipolevoy@gmail.com on 19 Aug 2010 at 4:47

GoogleCodeExporter commented 9 years ago
Yes this is useful if sequences are managed in some external system.

Original comment by javain...@gmail.com on 3 Aug 2011 at 6:36

GoogleCodeExporter commented 9 years ago
Or IDs are generated via an external system like snowflake, noeq, or even just 
a uuid.

I need to implement this feature for my employer.  Is there a preferred way to 
handle this?  I tried setId() from inside of an onBeforeCreate callback (which 
fires after the null-check that determines insert vs update) but it doesn't 
actually save the new id to the database because it's explicitly excluded by 
Model.getValueAttributeNames().  I suspect an annotation like 
ManualIdManagement could be created to disable that exclusion.

Alternately, an overridable generateId() method could be created in the Model 
that somehow gets called from DefaultDialect.createParametrizedInsert()

Is there another methodology that you would prefer?  We need this feature soon, 
so I will begin working on a patch as soon as I know which is the best way to 
proceed.

Original comment by ibex...@gmail.com on 9 Nov 2012 at 8:46

GoogleCodeExporter commented 9 years ago
can you please explain the case for this?

Original comment by i...@polevoy.org on 9 Nov 2012 at 10:20

GoogleCodeExporter commented 9 years ago
Mostly for asynchronous distributed systems (e.g. client-genenerated ids, 
out-of-sync databases, or as commenter 2 said, id values managed by an external 
system) and fault tolerance (continued limited operation while database is 
inaccessible).

It'd just be easier to use the existing "id" methods in activejdbc rather than 
create a separate "the_real_id" column in every table.

As for our actual use case, we'll be using noeqd (https://github.com/noeq), 
which was originally written for internal use at Heroku, and is based on a 
similar system used by Twitter.

Original comment by ibex...@gmail.com on 9 Nov 2012 at 10:32

GoogleCodeExporter commented 9 years ago
I suppose I can implement it by exposing a new Model.insert() method, so you 
could:

model.setId(obj);
model.insert();

Implementing it with the save() method carries a risk of potential breaking 
thousands of projects. 
I started implementing it like this

Original comment by i...@polevoy.org on 11 Nov 2012 at 7:26

GoogleCodeExporter commented 9 years ago
this was fixed, and a new spanshot deployed to: 
https://oss.sonatype.org/content/repositories/snapshots/org/javalite/activejdbc/
1.4.6-SNAPSHOT/
Please, test and let me know if this is working out for you 

Original comment by i...@polevoy.org on 12 Nov 2012 at 8:51