Skip to content

String ID aliases #167

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

Open
greenrobot opened this issue Sep 4, 2017 · 60 comments
Open

String ID aliases #167

greenrobot opened this issue Sep 4, 2017 · 60 comments
Labels
enhancement New feature or request

Comments

@greenrobot
Copy link
Member

Offer some convenience to use strings in an ID fashion.

@greenrobot greenrobot added the enhancement New feature or request label Sep 11, 2017
@thomasdao
Copy link

I really hope this can be supported soon, love to try it out but having to use long id is quite troublesome

@wirusw
Copy link

wirusw commented Oct 28, 2017

Would be really, really appreciated if this was added!

@Mistic92
Copy link

Any ETA? Month or week? I need to know this before public app release ;)

@vadimh77
Copy link

Hi guys,

It will great to support the string ids like other ORMs.

In the current situation if we get entities from the server that already exist in the DB then we don't know about it until making the call to the DB and finding these long ids and then copy them... (not a nice code)

@silviahisham
Copy link

any news about when will this be implemented?

@motornyimaksym
Copy link

Of course there are some disadvantage in performance when string is used for primary key. But out of doubt such possibility should be. It's personal responsibility for everyone to use string as id or not. It's good if we have a choice what is more important performance or implementation speed. I wasn't able to use Room from google because it hasn't relations between entities. They explained that all relations should be handled on server side. It's true and it's best practice. But what if I'm not able to change server side because it's third part service. I'm pretty sure that some amount of people can't use ObjectBox because it can't handle string as id. Looking forward!

@obianachebe
Copy link

obianachebe commented Feb 25, 2018

In the meantime, has anyone come up with a workaround for this? At the moment, all I can think of is to persist a separate id for MongoDB objects

EDIT
Looks like this is the recommended workaround from the docs:

If your application requires other ID types (such as a string UID given by a server), you can model them as standard properties and use queries to look up entities by your application specific ID.

@Entity public class User { @Id private long id; @Index private String serverUid; }

@motornyimaksym
Copy link

@obianachebe My workaround in kotlin:
fun String.toId(): Long = this.hashCode().toLong()

@greenrobot
Copy link
Member Author

@Yazon2006 Dangerous thing to do. Hash collisions are quite likely.

@Mistic92
Copy link

I was going to write the same. But for now, there is no other easy solution. Separate field for storing String id make a lot of mess and each time you need to check if it's not already in db.

@motornyimaksym
Copy link

motornyimaksym commented Feb 27, 2018

@greenrobot Of course I'm familiar with hash collisions. But in my case it's not a problem 'cause I have exhaustive amount of data in my db and there are no hash collisions. And also I hope that the feature with string id in objectBox will be available until release of my application.

@james-ff
Copy link

+1 for String Ids

@longforus
Copy link

expectation

@sandsaber
Copy link

+1
And my current workaround:
(Yeah, negative Id is still id, and work fine :) )

 fun longHash(string: String): Long {
        var h = 98764321261L
        val l = string.length
        val chars = string.toCharArray()

        for (i in 0 until l) {
            h = 31 * h + chars[i].toLong()
        }
        return h
    }

@vadimh77
Copy link

Can you please provide any time estimation for the String type ids implementation?

@a2open
Copy link

a2open commented Jun 23, 2018

Is there a development message about this feature?

@ugurcany
Copy link

+1

2 similar comments
@zhenxun815
Copy link

+1

@alijaza
Copy link

alijaza commented Jul 24, 2018

+1

@greenrobot
Copy link
Member Author

Btw, a good in-between state is to use @Unique as introduced with ObjectBox 2.0.

@linguokun1
Copy link

@greenrobot
That's so Good

@thomasdao
Copy link

I can see @Unique is useful but it's not a replacement for String id. Let say if we want to sync the objects with string id from server, we would first need to query for existing object in local db then save or update the object in local db. I don't think it's performant approach, unless we load all the objects in db into memory so we can avoid the query for existing object.

@avcial
Copy link

avcial commented Aug 2, 2018

@thomasdao +111111
Unfortunately we have to use server-Uniq-identifiers, this is the only way for us,
Please pretty please release this feature immediately.
God bless greenrobot :)

@linguokun1
Copy link

linguokun1 commented Aug 3, 2018

@greenrobot
when I want to update the same unique String vin property's entity, it throw : "Caused by: io.objectbox.exception.UniqueViolationException: Unique constraint for VehicleConfiguration.vin would be violated by putting entity with ID 2 because same property value already exists in entity with ID 1"

@linguokun1
Copy link

linguokun1 commented Aug 3, 2018

@greenrobot
So I must remove the entity with unique vin property and put again rather than can cover update it?

@avcial
Copy link

avcial commented Aug 3, 2018

@linguokun1
They should at least put the already exist entity into exception instance.

@avcial
Copy link

avcial commented Sep 28, 2018

maybe not my bussiness but if you are afraid about limitless strings to produce identifiers, you may firstly consider UUID's as an elemtary type and make them indexable. Since lot's of developers uses Guid's as public indexes or tokens

@greenrobot
Copy link
Member Author

Thanks for your suggestion. We prefer long IDs for efficiency. For sync, we use something like UUIDs internally (there's probably no use in exposing those).

@Sardoan
Copy link

Sardoan commented Dec 14, 2018

For sync with other databases we would need the UUIDs. Seems you see the point in internal use, but using it for syncing with backend (uses UUID), iOS (uses UUID) and Android (with this DB long :( Beeing offline and using a long as id is no good idea. An UUID is the better way.

@VictorQueiroz
Copy link

VictorQueiroz commented Dec 18, 2018

This is a must for me, would be great if you could support that. Specially for those receiving objects coming from a MongoDB database. Unfortunately I'll have to stick with greenDAO for now until this is a supported feature.

@avcial
Copy link

avcial commented Dec 18, 2018

still waiting for.....

@jesualex
Copy link

jesualex commented Mar 22, 2019

+1

@lucamtudor
Copy link

Any updates?

@andreiverdes
Copy link

What he said ^

@lucamtudor
Copy link

Are you guys actively working on this? This is a major pain-point for us!

We can't properly use relations in our models and let ObjectBox figure out when an object is new or needs updates, so we have to do a manual look-up each time we want to save something.

We used Realm & started migration to ObjectBox because of better API, better support for Architecture Components & no weirdness caused by the no-copy model. And because Realm is so damn slow at implementing features requested by the community (there still are a couple of issues from 2017 I'm still waiting for).

We're building a chat platform with lots of features that would benefit from ObjectBox's upcoming sync. But if I can't build the app today without making lots of error & bug prone workarounds there won't be a day when we'll happily pay for the sync service.

I moved away from SQL in 2014 and never looked back. Realm worked pretty well for not very complex scenarios & it was still better than writing tons of boilerplate code for SQL. ObjectBox came along at it seemed to be something even better, especially in the context of Android Architecture Components. But for a slightly more complex scenario that doesn't use Long ids, everything falls apart. I'm seriously considering going back to SQL. Room became a pretty solid library. It even supports suspend functions!

If the ObjectBox team is not interested in working on this, or has other priorities, just say so! Don't keep us waiting for months on end, for nothing.

Thanks, and sorry for the long & ranty message!

@vitaliecroitor
Copy link

Any updates regarding this issue?

The ToOne/ToMany solves only a part of the problem. Can we have an update strategy for relations?
Someting like: @OnUpdate(REPLACE).

Thanks!

@greenrobot-team
Copy link
Member

Thanks for all the requests on an update. Our focus is currently on sync. And as always I can't give timelines, sorry.
-Uwe

@linuxanton-2469
Copy link

After two years of waiting, please give us a definitive timeline when string IDs will be implemented.

Customers are waiting for results. Otherwise i have to go back to SQL and never come back to Objectbox.

Regards
Anton

@mesterj
Copy link

mesterj commented Oct 29, 2019

Room is a very good alternative for objectbox.

@DaveBoy
Copy link

DaveBoy commented Dec 18, 2019

i get this issues before i use objectbox,it be created before 2years ago,i don't konw whether to choose it

@rsktash
Copy link

rsktash commented Jan 24, 2020

Are you going to solve this issue? We android developers don't have much more alternatives. At this time room sqlite is the best choice for us.

@edeuss
Copy link

edeuss commented Oct 22, 2021

This would be a great feature do I don't have to generate two id's to store from server to local database.

@NaikSoftware
Copy link

NaikSoftware commented Oct 31, 2021

Are you going to solve this issue? We android developers don't have much more alternatives. At this time room sqlite is the best choice for us.

What about Realm? I used on large Android project and its great fast and easy to use DB. Now searching database for Flutter project and Realm does not support it now. I trying ObjectBox but it seems as raw and unfinished library:(

@jonasrotilli
Copy link

This is really an important limitation. The possibility of ID with String is very useful for everyone, it's past time for this to be implemented, it's been 4 years of countless requests and justifications.
Many have already given up and are migrating to other platforms, I am reluctant to do that, but the pressure is great as everyone else implements this basic option.

@rsktash
Copy link

rsktash commented Nov 18, 2021

No need to use this lib.
I have a two option for kotlin mltiplatform project first sqldelight second realm kotlin

@greenrobot-team
Copy link
Member

Just pointing out that 3.0 shipped with support for using @Unique(onConflict = ConflictStrategy.REPLACE). It is then possible to create an entity like:

@Entity
data class Example(
        @Id
        var id: Long = 0,
        @Unique(onConflict = ConflictStrategy.REPLACE)
        var uniqueKey: String? = null
)

On put, if an object with the String key already exists it will then be removed and a new one inserted.

This may already cover some use cases mentioned above.

@DaveBoy

This comment was marked as resolved.

@greenrobot-team

This comment was marked as resolved.

@wvffle

This comment has been minimized.

@ziaratban

This comment has been minimized.

@vaishnavmhetre

This comment has been minimized.

@sonic-onkaringale

This comment was marked as spam.

@vaishnavmhetre
Copy link

The issue was opened on 4th September 2017.
It's March 2023, any update?

Dont bother mate they do nothing but mark as spam 😊

Good luck objectbox.

@greenrobot-team
Copy link
Member

greenrobot-team commented May 15, 2023

If there is an update, like the last one, we will share it. Please only comment if you have something to contribute (e.g. an implementation suggestion, a use case that wasn't mentioned or where the suggested workaround does not work). Otherwise this produces needless work for us (e.g. me checking all new comments for something relevant).

And I hid such comments above so it is easier for other users coming here to find the suggested workaround.

If you are interested in this feature, please thumbs up the first post. This helps us track interest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests