I use ormlite in android and have successfully created the tables and perform various operations using the DAO.
But I am stuck in deleting a row of foreign keys if the primary key is deleted from the main table.
I have two tables named Parent and child. A parent has more than one child, so I associated it with a foreign key.
The code:
Parent table:
@DatabaseField(id = true) private Integer id; @ForeignCollectionField(eager = false) private ForeignCollection<Child> childCollection;
Children table:
@DatabaseField(id = true) private Integer id; @DatabaseField(foreign = true, foreignAutoRefresh = true, canBeNull = false, index = true, columnDefinition = "INTEGER REFERENCES parent(id) ON DELETE CASCADE") private Parent parent;
Now, if I delete the parent row for a specific identifier, this does not remove the children from the child table.
public void deleteById(Integer parentId) { try { Dao<Parent, Integer> parentDao = databaseHelper.getParentDao(); parentDao .deleteById(parentId); } catch (SQLException e) { e.printStackTrace(); } }
I beg you if I am mistaken. I tried Google many times, but no luck.
source share