DBFlow icon indicating copy to clipboard operation
DBFlow copied to clipboard

auto increment not work

Open rexi1r opened this issue 11 years ago • 5 comments

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 comments;

@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 avatar Oct 15 '15 20:10 rexi1r

@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.

agrosner avatar Oct 15 '15 21:10 agrosner

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.

agrosner avatar Oct 15 '15 21:10 agrosner

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 a = new 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 ....

rexi1r avatar Oct 15 '15 22:10 rexi1r

you can do that via a loop with a counter. This is not expected behavior for autoincrement.

agrosner avatar Nov 15 '15 18:11 agrosner

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.

brwskitime avatar Jan 19 '16 18:01 brwskitime