我在 Swift 中使用 SharkORM 创建一个 SQLite 数据库,问题是我有两个使用外键具有一对多关系的表,我需要知道是否有一种方法可以在删除时实现级联这两张表,
当我从主表中删除一条记录时,明细表中所有带有外键的记录应该自动删除,我不想手动实现。
那么有没有办法使用SharkORM实现级联删除呢?我已经查看了文档,但找不到任何东西,
预先感谢您的帮助。
最佳答案
因此,如果您以 Person -> Department
为例,其中 person 包含一个 .department
属性。然后,您通过 entityWillDelete
方法级联这些删除。
一个例子是像这样的“东西”。
class Department: SRKObject {
...
override func entityWillDelete() -> BOOL {
Person.query()
.whereWithFormat("department = %@", parameters:[self])
.fetch()
.removeAll()
return true
}
}
对于人际关系,总是看反面。
entityWillDelete
的文档在此处提到了这一点。
/**
* Before SharkORM attempts an operation it will ask the
* persitable class if it would like to continue with this operation.
*
* @return BOOL if YES is returned then SharkORM WILL complete the operation
* and it is guaranteed to complete. All pre-requisite checks have been made
* and the statement compiled before getting to this point.
* It is safe to use this method to cascade operations to other classes.
* In the case of delete, you might wish to delete related records, or
* indeed remove this object from related tables.
*/
- (BOOL)entityWillDelete;
这一切都在一个事务中完成,包括所有级联和事件触发器更改。然后可以在出现故障/错误/中止的情况下全部回滚。
关于ios - Swift - 在 SharkORM 中级联删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43119650/