I use Android Room library. I have entity Products:
@Entity(tableName = "products", foreignKeys = @ForeignKey(
entity = Category.class,
parentColumns = "code",
childColumns = "category_id"))
public class Product {
@PrimaryKey(autoGenerate = true)
private Long id;
@ColumnInfo(name = "name")
private String name;
@ColumnInfo(name = "description")
private String description;
@ColumnInfo(name = "category_id")
private String categoryId;
and second entity.
@Entity(tableName = "categories")
public class Category {
@PrimaryKey
@NonNull
private String code;
@ColumnInfo(name = "name")
private String name;
@Ignore
private List<Product> products;
When I try insert list of products:
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
MyApplication.get().getDB().productDao().insertAll(productList);
return null;
}
}.execute();
I get error when try insert products to DB.
Caused by: android.database.sqlite.SQLiteConstraintException: foreign key constraint failed (code 19)
How can I fix it?