I have been using the Android Studio App for about a month now,in order to create a project for a university subject. Unfortunatelly,I came across a problem while I was trying to create a database using Room that consists of 3 tables. Just to let you know,I have read all the threads reffering to this problem and unfortunatelly I couldn't solve it...
@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();
}
CodePudding user response:
Usually, Foreign key constraint failed happens when you try to constraint child column to an empty or null
parent column, this usually happens when :
- The child entity inserted to the database before the parent entity
- The
childColumns
value doesn't match anyparentColumns
value