Android Room错误:FOREIGN KEY约束失败(代码787)

时间:2019-06-12 07:20:30

标签: android android-room

我是Android Room的新手。我正在学习它,所以我尝试开发一个演示应用程序 类名称:Details其中 idnamemobile, 地址类对象, 父类对象 // Getter和Setter

Address class{
id,
city,
district,
state,
pin-code,
address type
}
Parent class{
id,
Father name,
Mother name
}

我写了一些代码,但是出错了:

  

外键约束失败

我尝试使用堆栈溢出中给出的解决方案,但仍然收到错误。 请帮忙!

public interface ItemDAO {

    @Insert
     public void insert(Details1 details);

    @Update
    public void update(Details1 details);

    @Delete
    public void delete(Details1 details);

    @Query("SELECT * FROM details ORDER BY id")
    public List<Details1> getDetails();

    @Query("SELECT * FROM details WHERE id = :id")
    public Details1 getDetailsById(Long id);
}




@Entity
(tableName = "details",
foreignKeys = {@ForeignKey(entity = Address1.class,childColumns = "add1_id",parentColumns = "id"),

@ForeignKey(entity = Parent1.class,childColumns = "Parent1_id",parentColumns = "id")})

public class Details1 {
    @PrimaryKey (autoGenerate = true)

    @ColumnInfo(name="id")
    private int details1_id;
    @ColumnInfo(name="Name")
    private String name;
    @ColumnInfo(name = "Mobile")
    private long mobile;
    @ColumnInfo(name="Parent1_id")
    private int pare1_id;
    @ColumnInfo(name="add1_id")
    private int Address1_id;

    public void setAddress1_id(int address1_id) {
        Address1_id = address1_id;
    }

    public int getAddress1_id() {
        return Address1_id;
    }

    public void setPare1_id(int pare1_id) {
        this.pare1_id = pare1_id;
    }

    public int getPare1_id() {
        return pare1_id;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDetails1_id(int details1_id) {
        this.details1_id = details1_id;
    }

    public int getDetails1_id() {
        return details1_id;
    }

    public void setMobile(long mobile) {
        this.mobile = mobile;
    }

    public long getMobile() {
        return mobile;
    }

Main Activity.java

        button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            appDatabase = AppDatabase.getAppdatabase(getApplicationContext());

            ItemDAO itemDAO = appDatabase.getItemDAO();
            Parent1DAO parent1DAO = appDatabase.getparent();
            Address1DAO address1DAO = appDatabase.getAddress();

            Details1 details = new Details1();
            Address1 address = new Address1();
            Parent1 parent = new Parent1();

            details.setName(edt1.getText().toString());
            details.setMobile(Long.parseLong(edt2.getText().toString()));

            address.setCity(edt3.getText().toString());
            address.setDistrict(edt4.getText().toString());
            address.setState(edt5.getText().toString());


          address.setPincode(Integer.parseInt(edt6.getText().toString()));
            address.setAddressType(edt7.getText().toString());

            parent.setFatherName(edt8.getText().toString());
            parent.setMotherName(edt9.getText().toString());

            itemDAO.insert(details);
            parent1DAO.insert(parent);
            address1DAO.insert(addresss);}
    });

AppDatabase.class

公共抽象类AppDatabase扩展了RoomDatabase {

private static String MY_DB = "mydb";
private static AppDatabase INSTANCE;

public static AppDatabase getAppdatabase(Context context){
    final File dbFile = context.getDatabasePath(MY_DB);
    if(dbFile.exists()){
        INSTANCE = Room.databaseBuilder(context,
                AppDatabase.class, "mydb")
                .addMigrations(MIGRATION_1_2)
                .allowMainThreadQueries().build();
        //This is Used when Database mmigration occurs.
    } else {
        INSTANCE = Room.databaseBuilder(context, AppDatabase.class, "mydb").allowMainThreadQueries().build();

    }
    return INSTANCE;
}

static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(@NonNull SupportSQLiteDatabase database) {
        //Since we don't alter the table so there is no migration
    }
};

public abstract ItemDAO getItemDAO();
public abstract Address1DAO getAddress();
public abstract Parent1DAO  getparent();

}

Address1.class


 @Entity(tableName = "address1")
    public class Address1 {
        @PrimaryKey(autoGenerate = true)
        @ColumnInfo(name = "id")
        private int Address_id;
        @ColumnInfo(name = "City")
        private String city;
        @ColumnInfo(name = "District")
        private String district;
        @ColumnInfo(name = "State")
        private String state;
        @ColumnInfo(name = "pin")
        private long pincode;
        @ColumnInfo(name = "address")
        private String addressType;
    @ColumnInfo(name="Detai_id")
    private int Detail_id;

        public void setDetail_id(int detail_id) {
            Detail_id = detail_id;
        }

        public int getDetail_id() {
            return Detail_id;
        }

        public void setAddress_id(int address_id) {
            Address_id = address_id;
        }

        public int getAddress_id() {
            return Address_id;
        }

        public void setState(String state) {
            this.state = state;
        }

        public String getState() {
            return state;
        }

        public void setPincode(long pincode) {
            this.pincode = pincode;
        }

        public long getPincode() {
            return pincode;
        }

        public void setDistrict(String district) {
            this.district = district;
        }

        public String getDistrict() {
            return district;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getCity() {
            return city;
        }

        public void setAddressType(String addressType) {
            this.addressType = addressType;
        }

        public String getAddressType() {
            return addressType;
        }
    }

Parent1.class

@Entity(tableName = "parent1")
public class Parent1 {
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    private int Parent_id;
    @ColumnInfo(name = "father_name")
    private String fatherName;
    @ColumnInfo(name = "mother_name")
    private String MotherName;
    @ColumnInfo(name = "det_id")
    private int Det_id;

    public void setDet_id(int det_id) {
        Det_id = det_id;
    }

    public int getDet_id() {
        return Det_id;
    }

    public void setParent_id(int parent_id) {
        Parent_id = parent_id;
    }

    public int getParent_id() {
        return Parent_id;
    }

    public void setFatherName(String fatherName) {
        this.fatherName = fatherName;
    }

    public String getFatherName() {
        return fatherName;
    }

    public void setMotherName(String motherName) {
        MotherName = motherName;
    }

    public String getMotherName() {
        return MotherName;
    }

    @Override
    public String toString() {
        return "Item{" +
                "id=" + Parent_id +
                ", father name='" + fatherName + '\'' +
                ", Mother name='" + MotherName + '\'' +
                '}';
    }

Details1.class

@Entity(tableName = "details",foreignKeys = {@ForeignKey(entity = Address1.class,childColumns = "add1_id",parentColumns = "id"),
        @ForeignKey(entity = Parent1.class,childColumns = "Parent1_id",parentColumns = "id")})
public class Details1 {
    @PrimaryKey (autoGenerate = true)
    @ColumnInfo(name="id")
    private int details1_id;
    @ColumnInfo(name="Name")
    private String name;
    @ColumnInfo(name = "Mobile")
    private long mobile;
    @ColumnInfo(name="Parent1_id")
    private int pare1_id;
    @ColumnInfo(name="add1_id")
    private int Address1_id;

    public void setAddress1_id(int address1_id) {
        Address1_id = address1_id;
    }

    public int getAddress1_id() {
        return Address1_id;
    }

    public void setPare1_id(int pare1_id) {
        this.pare1_id = pare1_id;
    }

    public int getPare1_id() {
        return pare1_id;
    }

    /*
                    private ArrayList<Address1> addresses;

                    private Parent1 parent;
                */
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDetails1_id(int details1_id) {
        this.details1_id = details1_id;
    }

    public int getDetails1_id() {
        return details1_id;
    }

    public void setMobile(long mobile) {
        this.mobile = mobile;
    }

    public long getMobile() {
        return mobile;
    }
/*
    public void setAddresses(ArrayList<Address1> addresses) {
        this.addresses = addresses;
    }

    public ArrayList<Address1> getAddresses() {
        return addresses;
    }

    public void setParent(Parent1 parent) {
        this.parent = parent;
    }

    public Parent1 getParent() {
        return parent;
    }
    */
@Override
public String toString() {
    return "Item{" +
            "id=" + details1_id +
            ", name='" + name + '\'' +
            ", Mobile no='" + mobile + '\'' +
            '}';

}
}


1 个答案:

答案 0 :(得分:0)

昨天我刚刚遇到了同样的错误。如果父Entity上没有记录,则不能在该表上写任何东西。这就是发生错误的原因。因此,请确保在使用

之前

public void insert(Details1 details);,请确保您要插入Address1类(或父类)中的值。

还请在ItemDAO上标记@Dao,以便Room知道发生了什么。

修改 更改以下行:

        itemDAO.insert(details);
        parent1DAO.insert(parent);
        address1DAO.insert(addresss);

收件人:

    parent1DAO.insert(parent);
    address1DAO.insert(addresss);
    itemDAO.insert(details);