外键约束失败(代码 787 sqlite_constraint 外键)

foreign key constraint failed (code 787 sqlite_constraint foreign key)

我已经使用 Android Studio 应用程序大约一个月了,目的是为大学学科创建一个项目。 不幸的是,我在尝试使用包含 3 个表的 Room 创建数据库时遇到了一个问题。 只是想让你知道,我已经阅读了所有关于这个问题的线程,不幸的是我无法解决它...

@Entity(tableName = "travelAgencies")
public class travelAgency {
    @PrimaryKey
    @ColumnInfo(name = "tACode")
    private int agencyCode;

@ColumnInfo(name = "tAName")
private String agencyName;

@ColumnInfo(name = "tAAddress")
private String agencyAddress;

public int getAgencyCode() {
    return agencyCode;
}

public void setAgencyCode(int agencyCode) {
    this.agencyCode = agencyCode;
}

public String getAgencyName() {
    return agencyName;
}

public void setAgencyName(String agencyName) {
    this.agencyName = agencyName;
}

public String getAgencyAddress() {
    return agencyAddress;
}

public void setAgencyAddress(String agencyAddress) {
    this.agencyAddress = agencyAddress;
}





    @Entity(tableName = "suggetedVacations")
public class suggestedVacation {

@PrimaryKey
@ColumnInfo(name = "sVCode")
private int suggestedVacationCode;

@ColumnInfo(name = "sVCity")
private String suggestedVacationCity;

@ColumnInfo(name = "sVCountry")
private String SuggestedVacationCountry;

@ColumnInfo(name = "sVDuration")
private String SuggestedVacationDuration;

@ColumnInfo(name = "sVType")
private String SuggestedVacationType;

public int getSuggestedVacationCode() {
    return suggestedVacationCode;
}

public void setSuggestedVacationCode(int suggestedVacationCode) {
    this.suggestedVacationCode = suggestedVacationCode;
}

public String getSuggestedVacationCity() {
    return suggestedVacationCity;
}

public void setSuggestedVacationCity(String suggestedVacationCity) {
    this.suggestedVacationCity = suggestedVacationCity;
}

public String getSuggestedVacationCountry() {
    return SuggestedVacationCountry;
}

public void setSuggestedVacationCountry(String suggestedVacationCountry) {
    SuggestedVacationCountry = suggestedVacationCountry;
}

public String getSuggestedVacationDuration() {
    return SuggestedVacationDuration;
}

public void setSuggestedVacationDuration(String suggestedVacationDuration) {
    SuggestedVacationDuration = suggestedVacationDuration;
}

public String getSuggestedVacationType() {
    return SuggestedVacationType;
}

    public void setSuggestedVacationType(String suggestedVacationType) {
        SuggestedVacationType = suggestedVacationType;
    }
}





@Entity(tableName ="theVacationPacket",
    primaryKeys = {"tVPAgencyCode","tVPVacationCode","tVPCode"},
    foreignKeys = {
    @ForeignKey(entity = travelAgency.class,
                parentColumns = "tACode",
                childColumns = "tVPAgencyCode",
                onDelete = ForeignKey.CASCADE,
                onUpdate = ForeignKey.CASCADE),
    @ForeignKey(entity = suggestedVacation.class,
                parentColumns = "sVCode",
                childColumns = "tVPVacationCode",
                onDelete = ForeignKey.CASCADE,
                onUpdate = ForeignKey.CASCADE)})
public class vacationPacket {
    @ColumnInfo(name = "tVPCode") @NonNull
    private int tableVacationPacketCode;

    @ColumnInfo(name = "tVPAgencyCode") @NonNull
    private int tableVacationPacketAgencyCode;

    @ColumnInfo(name = "tVPVacationCode") @NonNull
    private int tableVacationPacketVacationCode;

    @ColumnInfo(name = "tVPDateOfDeparture")
    private String tableVacationPacketDateOfDeparture;

    @ColumnInfo(name = "tVPPrice")
    private double tableVacationPacketPrice;

    public int getTableVacationPacketCode() {
            return tableVacationPacketCode;
    }

    public void setTableVacationPacketCode(int tableVacationPacketCode) {
            this.tableVacationPacketCode = tableVacationPacketCode;
    }

    public int getTableVacationPacketAgencyCode() {
            return tableVacationPacketAgencyCode;
    }

    public void setTableVacationPacketAgencyCode(int tableVacationPacketAgencyCode) {
            this.tableVacationPacketAgencyCode = tableVacationPacketAgencyCode;
    }

    public int getTableVacationPacketVacationCode() {
            return tableVacationPacketVacationCode;
    }

    public void setTableVacationPacketVacationCode(int tableVacationPacketVacationCode) {
            this.tableVacationPacketVacationCode = tableVacationPacketVacationCode;
    }

    public String getTableVacationPacketDateOfDeparture() {
            return tableVacationPacketDateOfDeparture;
    }

    public void setTableVacationPacketDateOfDeparture(String tableVacationPacketDateOfDeparture) {
            this.tableVacationPacketDateOfDeparture = tableVacationPacketDateOfDeparture;
    }

    public double getTableVacationPacketPrice() {
            return tableVacationPacketPrice;
    }

    public void setTableVacationPacketPrice(double tableVacationPacketPrice) {
            this.tableVacationPacketPrice = tableVacationPacketPrice;
    }


    @Database(entities ={suggestedVacation.class,travelAgency.class,vacationPacket.class},version = 1)
public abstract class roomApiDatabase extends RoomDatabase {
    public abstract RoomApiDao roomApiDaoVariable();
}



    @Dao
public interface RoomApiDao {
    @Insert
    public void insertTravelAgency(travelAgency travelAgency);

@Update
public void updateTravelAgency(travelAgency travelAgency);

@Delete
public void deleteTravelAgency(travelAgency travelAgency);

@Insert
public void insertSuggestedVacation(suggestedVacation suggestedVacation);

@Update
public void updateSuggestedVacation(suggestedVacation suggestedVacation);

@Delete
public void deleteSuggestedVacation(suggestedVacation suggestedVacation);

@Insert
public void insertVacationPacket(vacationPacket vacationPacket);

@Update
public void updateVacationPacket(vacationPacket vacationPacket);

@Delete
public void deleteVacationPacket(vacationPacket vacationPacket);

@Query("select * from travelAgencies")
public List<travelAgency> getTravelAgency();

@Query("select * from suggetedVacations")
public List<suggestedVacation> getSuggestedVacations();

@Query("select * from theVacationPacket")
public List<vacationPacket> getVacationPackets();

}

通常,当您尝试将子列约束到 emptynull 父列时,会发生外键约束失败, 这通常发生在 :

  1. 先于父实体插入数据库的子实体
  2. childColumns 值不匹配任何 parentColumns