When you use Android Room auto generated insert method, it pass all values to all columns For example, if you have an entity like below:
@Entity
public class Task {
@PrimaryKey(autoGenerate = true)
public long id;
@NonNull
@ColumnInfo(defaultValue = "Unknown Title")
public String name;
@NonNull
@ColumnInfo(defaultValue = "Does not have any description!")
public String desc;
@ColumnInfo(defaultValue = "false")
public boolean isDone;
}
And you try to insert an empty instance of Task
class by:
taskDao.insert(new Task());
It will run a query like this:
INSERT INTO Task (id, name, desc, isDone) values (null, null, null, 0);
that is against of our table structure rules, so we get error:
Caused by: android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: task.name (code 1299 SQLITE_CONSTRAINT_NOTNULL)
While if it use this query:
INSERT INTO Task DEFAULT VALUES;
or
INSERT INTO Task (id) VALUES (null);
SQLite creates a row with all default values, without any errors.
But to ignore null variables in the insert query for using default valuse?