auto increment not work
hi
i have a model that fill with JSON comes from server but my auto increment field always is zero.
here is my model:
@Table(tableName = Post.Schema.Post, databaseName = PostProvider.DB_NAME)
public class Post extends BaseModel implements IGenericResponse {
public String status;
public List
@Override
public String getStatus() {
return status;
}
@Override
public List<String> getComments() {
return comments;
}
@Column
public String created_at;
@Column
public String body;
@Column
@PrimaryKey(autoincrement = true)
public long _id;
@Unique(onUniqueConflict = ConflictAction.REPLACE)
@Column
public long id;
public List<Category> tags;
the JSON result from my server have a field with name i,d so i had to declare auto increment field with name _id.
@rexi1r that's the point of autoincrement. It increments the counter for every new value you save in the DB. It will only save the existing instance if it already exists in the database with that id. When inserting it ignores the value of the autoincrementing primary key.
so my suggestion is to use A regular int as primary key and figure out the largest id every time you need to insert a new value.
i need to simulate function like auto increment for my model that when i create an ArrayList of model that function increment id.for example
ArrayList
and after declare, fill the "a" with some data.
when i want to read the a data, it return the result like below:
a.get(0).id --> 0
a.get(1).id --> 1
....
you can do that via a loop with a counter. This is not expected behavior for autoincrement.
The problem I'm having is that if an autoincrement field has a value, that value is stomped on.
Here's my field:
@Column(name = _ID)
@NotNull
@PrimaryKey(autoincrement = true)
@SerializedName("id")
long _id;
Sometimes the _id field is populated via json from the server. When I do an insert/update, the value is always ignored. It should only autoincrement if _id is null or 0 in my case. This is how database autoincrement fields normally function. Think it would be better to just let the database handle autoincrementing.