Search code examples
androidactiveandroid

Active Android onDelete and onDelete


I use Active Android in my project. Trying to learn more about it, especial about table items properties -smth like ForeignKeyAction.

I want for my model to delete only itself, if delete action is occured, and not to delete it's children. Code I found -

@Column(name = "Category", onUpdate = ForeignKeyAction.CASCADE, onDelete = ForeignKeyAction.CASCADE)
public Category category;

But I do not know what flag answeirs for what property - there are plenty of them - form docs

public enum ForeignKeyAction {
    SET_NULL, SET_DEFAULT, CASCADE, RESTRICT, NO_ACTION
}

Can anyone post a link to detailed explanation, or explain this stuff. PS I explored lots of sites, inluding https://guides.codepath.com/android/ActiveAndroid-Guide and https://github.com/pardom/ActiveAndroid/wiki/Getting-started do not refer me there, there is no explanation on this question.

Also, what other properties, like onDelete i can set to my model fields?


Solution

  • These aren't properties of active android, but rather SQL. I'm guessing that active android utilizes the SQLite database in Android. If that's the case, here are their meanings:

    NO ACTION: Configuring "NO ACTION" means just that: when a parent key is modified or deleted from the database, no special action is taken.

    RESTRICT: The "RESTRICT" action means that the application is prohibited from deleting (for ON DELETE RESTRICT) or modifying (for ON UPDATE RESTRICT) a parent key when there exists one or more child keys mapped to it. The difference between the effect of a RESTRICT action and normal foreign key constraint enforcement is that the RESTRICT action processing happens as soon as the field is updated - not at the end of the current statement as it would with an immediate constraint, or at the end of the current transaction as it would with a deferred constraint. Even if the foreign key constraint it is attached to is deferred, configuring a RESTRICT action causes SQLite to return an error immediately if a parent key with dependent child keys is deleted or modified.

    SET NULL: If the configured action is "SET NULL", then when a parent key is deleted (for ON DELETE SET NULL) or modified (for ON UPDATE SET NULL), the child key columns of all rows in the child table that mapped to the parent key are set to contain SQL NULL values.

    SET DEFAULT: The "SET DEFAULT" actions are similar to "SET NULL", except that each of the child key columns is set to contain the columns default value instead of NULL. Refer to the CREATE TABLE documentation for details on how default values are assigned to table columns.

    CASCADE: A "CASCADE" action propagates the delete or update operation on the parent key to each dependent child key. For an "ON DELETE CASCADE" action, this means that each row in the child table that was associated with the deleted parent row is also deleted. For an "ON UPDATE CASCADE" action, it means that the values stored in each dependent child key are modified to match the new parent key values.

    In addition, here's a link where I found this information. You can also check out the general SQLite documentation.