I couldn\'t find any information how to annotate a SQL - \"DEFAULT\" value while looking into the @ColumnInfo docs for the new Android Persistence Library.
Does Room
For any one facing the situation when you have two foreign keys and "onDelete = CASCADE" you can set the foreign key to a data type that can be set to null for example:
int parent1Id = 0;
int parent2Id = 0;
//should be:
Long parent1Id = null;
Long parent2Id = null;
that way the DB knows that a particular Object/row has no other parent of different type when attempting to delete it when its parent is deleted.
You can check inside your getter method of Entity and set some default value there.
@Entity(tableName = "Dashboard")
public class Dashboard {
@PrimaryKey
@NonNull
@ColumnInfo(name = "claimNumber")
private String claimNumber;
private String percentage = "0";
private String imagePath = "";
@NonNull
public String getClaimNumber() {
return claimNumber;
}
public void setClaimNumber(@NonNull String claimNumber) {
this.claimNumber = claimNumber;
}
public String getPercentage() {
if (percentage == null || percentage.isEmpty()) {
return "0";
}
return percentage;
}
public void setPercentage(String percentage) {
this.percentage = percentage;
}
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
public Dashboard(@NonNull String claimNumber, String percentage, String imagePath) {
this.claimNumber = claimNumber;
this.percentage = percentage;
this.imagePath = imagePath;
}
}
Room hasn't any annotation for default value, but you can set default value in your entity like this:
@Entity(tableName = "MyTable")
class MyClass {
...
public String MyDefaultValuedCol = "defaultString";
public boolean MyDefaultFlagCol = true;
}
You can set the default value using @ColumnInfo annotation-
@ColumnInfo(defaultValue = "No name")
public String name;
and
@ColumnInfo(defaultValue = "0")
public int flag;
or for any kind of data types check the reference from here Google developer doc
With the release of room persistence 2.2.0, there's a new property added to @ColumnInfo annotation which can be used to specify the default value of a column. See documentation.
@Entity(tableName = "users")
data class User(
@PrimaryKey val id: Long,
@ColumnInfo(name = "user_name", defaultValue = "temp") val name: String
@ColumnInfo(name = "last_modified", defaultValue = "CURRENT_TIMESTAMP" ) val lastModified: String
)