How to auto-increment a field in ActiveAndroid ORM

How can I make an integer or long field to grow automatically using annotation.

+3


source to share


2 answers


As we can read in the documentation :

It's important to note that ActiveAndroid creates an id field for your tables. This field is automatically incremental to the primary key .

Perhaps access to an auto-generated primary key would be enough for you?



Also, if you want to create a custom primary key in your model, you can check out the solution mentioned in the ActiveAndroid-related GitHub issue that looks like this:

@Table(name = "Items", id = "clientId")
public class Item extends Model {
    @Column(name = "id")
    private long id;
}

      

Then the id field is a custom primary key that will automatically grow.

+1


source


In case of ActiveAndroid ORM, you don't need to write the ID of the column in the model, it will automatically generate an auto-incremented value and you can just use it. I am giving a sample model below -

@Table(name="Items")
public class Item extends Model{
    @Column(name="name")
    public String name;
}

      

Instead

@Table(name="Items")
public class Item extends Model{
    @Column(name="Id")
    public long id;
    @Column(name="name")
    public String name;
}

      



If the element is an Element object , you can simply get the id using

item.getId();

      

So the correct model is first. For reference, you can click here .

0


source







All Articles