Android : How to perform casecade deleting on foreign key using ormlite in android

on Friday, October 10, 2014


I am using ormlite in android and has created tables succesfully and perform the different operations using DAO.


But I have stuck in deleting the foreign keys row if primary key is deleted from the main table.


I have two tables named Parent and child. A parent have more than one child so I linked the child with foreign key.


Code:

Parent table:



@DatabaseField(id = true)
private Integer id;


@ForeignCollectionField(eager = false)
private ForeignCollection<Child> childCollection;


Child 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 am deleting parent row for a perticular id then this is not deleting 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();
}
}


Please guide me, where I am doing wrong. I have tried and Google many times but no luck.


Thanks


0 comments:

Post a Comment