Issues with negative IDs

16 views
Skip to first unread message

J. Julio Camacho Pachón

unread,
Dec 22, 2017, 4:06:42 PM12/22/17
to ORMLite Developers
I have a few classes to persist my entities, first locally and then to a remote database. To avoid conflicts, I've faced the problem of redundant IDs (the same one in the local and the remote databases) using negative values for the new objects created directly in the mobile device. So far so good. Obviously, I need to know what the minimun value already assigned is in order to assign the next one. Say, the last one was "-3", so the next one should be "-4". However, everytime I query for that minimun value, I always get "0" if there is a negative id already set.

I've tried doing this:

private int getTemporaryId() throws SQLException {
QueryBuilder<MyClass, Integer> builder = this.queryBuilder();
int result = -1;
builder
.selectRaw("min(" + MyClass.ID + ")").prepare();
String[] qryResult = builder.queryRawFirst();

if (qryResult[0] != null && Integer.parseInt(qryResult[0]) <= 0) {
result = Integer.parseInt(qryResult[0]) - 1;
}
return result;
}

and this:

private int getTemporaryId() throws SQLException {
QueryBuilder<MyClass, Integer> builder = this.queryBuilder();
int result = -1;
builder
.orderBy(MyClass.ID, true);
MyClass myObject = queryForFirst(builder.prepare());
if(myObject != null) {
result = myObject.getId()-1;
}
return result;
}

with the very same result: In the first case, I get the value "0" and in the second one, I get an object with its ID equals to "0". The thing is that if I run this sql sentence directly in a copy of the
local database:

select min(_id) from myTable;

then I get the expected negative value. So, my question is. Is there any way to select those negative values using OrmLite? Am I doing anything wrong?

J. Julio Camacho Pachón

unread,
Dec 23, 2017, 2:09:00 AM12/23/17
to ORMLite Developers
Nothing to do with the Ormlite framework. I was doing something wrong in another part of the code.
Reply all
Reply to author
Forward
0 new messages