Question

I'm using active android to work with SQLite on Android.

For example, I have the following Pets table with this structure:

@Table(name = "Pets")
public class Pet extends Model {

    @Column(name = "Name")
    public String name;

    @Column(name = "Gender")
    public String gender;

    @Column(name = "DoB")
    public Date dob;    

    public Pet() {
        super();
    }

    public Pet(String name, String gender, Date dob) {
        super();
        this.name = name;
        this.gender = gender;
        this.dob = dob;
    }

}

How do I show in a second table, called Achievements, that it has a foreign key to the primary_key of the Pets table.

This is I'm doing something as this

@Table(name = "Achievements")
public class Achievement extends Model {

    @Column(name = "name")
    public String name;

    @Column(name = "pet_id")
    public Pet pet;

    public Achievements() {
        super();
    }

    public Achievements(String name,Pet pet) {
         super();
         this.name = name;
         this.pet = pet;
    }
Was it helpful?

Solution

First off, you should double check your code here. I cleaned it up as best I could but it still looks like you have some errors in there, such as Achievements being spelled incorrectly and the Achievements constructor being plural.

To setup the relationship between an Achievement and the Pet record, you need to change the Pet column to be the following:

@Column(name = "Pet")
public Pet pet;

For more details take a look at this ActiveAndroid Wiki page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top