Home > OS >  Missing dependency to be able to use "onDelete = CASCASDE" on android?
Missing dependency to be able to use "onDelete = CASCASDE" on android?

Time:06-11

The title says it all. I'm trying to use the following code:

@ForeignKey
(entity = User.class, parentColumns = "id", childColumns = "user", onDelete = CASCADE)
private int user;

But for some reason Android Studio doesn't recognize "CASCADE". Do I need some dependency?

CodePudding user response:

Make sure that you have all of these dependencies for room database, and also use the latest version of room:

implementation "androidx.room:room-runtime:2.4.2"
kapt "androidx.room:room-compiler:2.4.2"
implementation "androidx.room:room-ktx:2.4.2"
androidTestImplementation "androidx.room:room-testing:2.4.2"

If you still having the problem so it's not a problem of dependencies

CodePudding user response:

Do I need some dependency?

You are using the @ForeignKey annotation in the incorrect place. ForeignKeys are defined as part of the @Entity annotation via the foreignKeys parameter.

Assuming that the class is ChildOfUser (for the same of demonstration) then use :-

@Entity(
        foreignKeys = {
                @ForeignKey(
                        entity = User.class,
                        parentColumns = "id", 
                        childColumns = "user", 
                        onDelete = ForeignKey.CASCADE, 
                        onUpdate = ForeignKey.CASCADE
                )
        }
)
class ChildOfUser {
    /* NOT HERE
    @ForeignKey(entity = User.class, parentColumns = "id", childColumns = "user", onDelete = ForeignKey.CASCADE)
    */
    @ColumnInfo(index = true) /* Room will issue warning if not indexed */
    private int user;
}
  • Related