Hi all, I am trying to make a `User` model object ...
# getting-started
j
Hi all, I am trying to make a
User
model object to be inserted into a database. I want to insert these into various databases using a Repository pattern, but the
User
has an
id
field that is generated differently for each database I use. Is there a way to defer the definition of the
id
type and a
generateId()
function until the actual creation of the
User
object in Kotlin? Thanks
g
No, it’s not possible direclty, but you alway can create a new instance of User object with new database id, and maybe on insert make id nullable (or some default value) Don’t say that it’s the best, but in general ORM approach is bad IMO and such issues as one you described haunt apps with ORMs forever
c
If you're able to change the record schema, I've recently been favoring UUIDs created and assigned in the application code instead of relying on anything generated by the database/ORM. It makes situations like this so much easier to work with. Java's got a built-in UUID class, or this library works well for multiplatform https://github.com/benasher44/uuid
1
j
Thanks for the feedback 😄 Nice to see I'm not the only one with headache from this issue. I'll look into UUID and other ways to avoid using ORM
g
Uuid is not the best solution for indexes, but depends on your scale
m
there are many variations of implementing unique identifiers, UUID just happens to be the most popular. Depending on your needs you may want to try out alternatives, for example if you need identifiers that are shorter, human-readable/memorizable, or sortable (ie ULID), etc. For example take a look here: https://github.com/sw-yx/uuid-list Here are some analysis on UUID: https://dzone.com/articles/uuid-as-primary-keys-how-to-do-it-right https://www.softwareatscale.dev/p/guids-are-not-enough
☝️ 1
👍 1
👍🏾 1
☝🏾 1