-
Notifications
You must be signed in to change notification settings - Fork 4
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
Generate ID on Entity.save() #8
Comments
Currently the The generator method sounds great. I'm thinking of having a However, there is the problem of pre-provided IDs (e.g. if the ID is pre-known, say, a state person identifier). The pre-provided ID kinda rules out the current |
I wonder if it's simply easier to replace In my simple apps I am always able to tell whether I'm creating or updating, and the first option sounds a lot simpler, both to explain in the API and to implement (no need to figure out how to do UPSERT on multiple databases), so I'm a bit biased that way. However, I'd love to draw from your experience @stapel - what do you think? Which way would work best for your app? |
Alternatively, we can perhaps keep all three methods. That way, you can use |
Personally I'd like the generateId()-method (I fiddled with it locally, I still need to find a way to add a custom UUID-Converter for sql2o). If going the create/update-way, I'd keep all three, because, as you said, save() will probably be perfect for most use cases. I wouldn't go in the UPSERT-direction, it sounds cumbersome to check for different databases with different versions.. and IIRC there is very little database-dependency (maybe a little pgsql-preference) in the vok-subprojects. What are your thoughs on always generating UUIDs on any |
Hmm, it could only work if the type of the ID column was UUID. I feel it's too magicky, in a sense that the behavior suddenly changes for UUID-typed column. Therefore I'd advise against it, I'd rather make it explicit. Since
I agree, I'll avoid UPSERT.
I thought it was natively supported... Let me test that as well. |
Great! Regarding UUIDs, in mysql you'd have to use binary(16) to properly save a UUID, this will be mapped to ByteArray in sql2o. And the default UUIDConverter does not check for a bytearray. So, writing works, reading doesn't. |
Yup, |
Fixed in vok-orm 0.17 |
Hmm, not fixed yet. Sql2o sometimes picks up the ID type as Object instead of UUID, doesn't activate the Converter and then fails that it can't set ByteArray value to a UUID field. |
This is a really nice solution, having only |
@stapel you don't even need to create the wrapper class perhaps; you can simply override the override fun create(validate: Boolean) {
id = UUID.randomUUID()
super.create(validate)
} then you can either call Yet I'd love to hear about your wrapper-class solution - could you paste a bit of info please? |
Reopened: rather than a nasty workaround in vok-orm it should be fixed in Sql2o: aaberg/sql2o#314 |
This bug report is about generating IDs; i've created a separate bug ticket regarding the failing converters: #10 |
@mvysny: It's nothing really, and I am not sure that this is the final form, but it keeps me from too much refactoring. ;) (And we probably want to use UUIDs for all Entities anyhow) (Edit: I probably used the wrong name with wrapper-class)
(Good catch with sql2o!) |
@stapel Hey, that's a really awesome idea, thanks! I've updated the docs to mention this possibility; we can probably simplify it to:
|
Currently
Entity
depends on the database the create a value forid
. This is nice for Longs or Ints as most databases provide autoincrement or something like that.Now I'd "want" to use UUIDs as
id
, this is hard to make work with e.g. MySQL in a convenient or simple way.I'd really like to see the functionality to provide a generator-method or something like this to make it into
Entity
.Any ideas or comments appreciated.
The text was updated successfully, but these errors were encountered: