当前位置: 首页 > 知识库问答 >
问题:

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

越飞翮
2023-03-14

我不熟悉Android房间。我正在学习它,所以我尝试开发一个演示应用程序类名:详细信息,它有id名称移动,地址类对象,父类对象//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;
    }

主要活动。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。班

公共抽象类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();

}

地址1。班


 @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;
        }
    }

父母1。班

@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个答案

呼延博易
2023-03-14

我昨天也遇到了同样的错误。如果父实体上没有记录,则无法在该表上写入任何内容。这就是错误发生的原因。因此,请确保在使用您的

公共作废插入(详情1详情),确保插入了地址1类(或父类)中的值。

另外,请用@Dao注释标记您的项目Dao,以便房间可以知道发生了什么。

编辑并更改以下行:

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

到:

    parent1DAO.insert(parent);
    address1DAO.insert(addresss);
    itemDAO.insert(details);
 类似资料:
  • 我在Amazon RDS上的mysql DB8.0.20版本中有两个表,它们之间有一个外键。 当我尝试插入第二个表时,我得到一个外键错误,但我确定第一个表中存在该键。 然后我跑: 那么,如果ID存在于表1中,为什么表2不能在外键列中使用它呢?我错过了什么?

  • 问题内容: 我想在AndroidRoom中用两个外键创建一个数据库。每次我尝试在数据库中插入曲目时,程序都会崩溃,并说“外键costraint失败(代码787)”。也许你们中的某人知道原因,可以为我提供帮助。 道道 “ Kategorie”和“ Playlist”也是数据库中的表。 卡特戈里 问题答案: 你需要有表中的行,并用相同的外键为具有要插入。

  • 我得到以下错误,但我不明白为什么我应该有一个索引。 错误代码:1822。添加外键Constaint失败。引用的表“users”中缺少约束“fk_articles_users1”的索引 我激活了 但这并没有显示出任何错误。

  • 问题内容: 尝试创建两个表时收到错误消息。有一个多值依赖项,因此我分离了表并提出了这个建议: 但是,当尝试使用此代码添加值时: 我收到臭名昭著的1452错误: 我不完全确定这意味着什么,因为我在第一个表中具有NameID自动递增功能。我不能在第二个中使用auto_increment,因为它是外键,对吗?在此先感谢您的帮助。 问题答案: 您已经在列上定义了外键约束,即在表中使用insert作为电话表

  • 问题内容: 我收到此错误消息: 第40行的错误1217(23000):无法删除或更新父行:外键约束失败 …当我尝试放置桌子时: …定义如下: 有趣的是, 我已经删除 了模式中具有外键的 所有其他表。实际上,除了表外,数据库是空的。 如果数据库中没有其他对象,怎么可能会有子行?据我所知,InnoDB不允许在其他模式上使用外键,对吗? (我什至可以运行命令:-?) 问题答案: 两种可能性: 在另一个架

  • 嗨,我知道有人问过这个问题,但我还没有找到正确的答案,为什么当我尝试做我的第二个插入: 我得到了错误: 错误1452(23000):无法添加或更新子行:外键约束失败(.,约束外键()引用()) 下面的DDL代码: