1

I have a table with two foreign key into two different tables

This is my table :

@Entity(
        tableName = Constants.TABLE_NAME_PICTURE,
        foreignKeys = {
        @ForeignKey(
                entity = BIN.class,
                parentColumns = "id",
                childColumns = "bin_id"
        ),
        @ForeignKey(
                entity = ORDER.class,
                parentColumns = "id",
                childColumns = "order_id"
        )},
        indices = {@Index("id"), @Index(value = {"bin_id","order_id"})})

public class PICTURE {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    public long id;
    @Attribute(name = "name", required = false)
    @ColumnInfo(name = "name")
    public String name;
    @ColumnInfo(name = "bin_id")
    public int binId;
    @ColumnInfo(name = "order_id")
    public int orderId;

and when I insert a PICTURE to the database I get :

android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787)

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
jpok
  • 143
  • 2
  • 2
  • 11

3 Answers3

1

i think you must change this lines

foreignKeys = [ForeignKey(
    entity = BIN::class,
    parentColumns = arrayOf("id"),
    childColumns = arrayOf("bin_id"),
    onDelete = ForeignKey.CASCADE
), ForeignKey(
    entity = ORDER::class,
    parentColumns = arrayOf("id"),
    childColumns = arrayOf("order_id"),
    onDelete = ForeignKey.CASCADE
)]
Radesh
  • 13,084
  • 4
  • 51
  • 64
0

It seems you did not add any row two parent table like BIN.class and Order.class. I got an answer from Here

Tariqul Islam
  • 680
  • 6
  • 14
  • I added a row BIN class and order class when I removed for example one forgein key It is work – jpok Dec 12 '18 at 10:16
  • But I have an question can I get null for forgein key ? some times i have pictore to order but sometimes to bin when for bin I past 0 to order_id but I do not have this value to order class – jpok Dec 12 '18 at 10:17
  • For joining multiple table you may get some null value (may be id) in right table. – Tariqul Islam Dec 12 '18 at 10:49
0

Try to do it like this

foreignKeys = [
        @ForeignKey(
                entity = BIN.class,
                parentColumns = "id",
                childColumns = "bin_id"
        ),
        @ForeignKey(
                entity = ORDER.class,
                parentColumns = "id",
                childColumns = "order_id"
        )],

Actually in kotlin I wrote this line in the following way

foreignKeys = arrayOf(
            @ForeignKey(
                    entity = BIN.class,
                    parentColumns = "id",
                    childColumns = "bin_id"
            ),
            @ForeignKey(
                    entity = ORDER.class,
                    parentColumns = "id",
                    childColumns = "order_id"
            )]),

May be it helps

Asset Bekbossynov
  • 1,633
  • 3
  • 13
  • 25
  • Ok but sometimes i put 0 to bin_id or order_id but I do not have this index in bin or order row maybe this is a mistake – jpok Dec 12 '18 at 10:21
  • but picture can be in table order or in table bin – jpok Dec 12 '18 at 10:21
  • @jpok About question that can `foreignKey` be null you may read this [answer](https://stackoverflow.com/a/49175873/9695678) – Asset Bekbossynov Dec 12 '18 at 10:23
  • So I have two created another table OrderPicture ? – jpok Dec 12 '18 at 10:25
  • @jpok If you about `Nullable` just change your `int` into `Integer`, however about creation table I do not understand your question – Asset Bekbossynov Dec 12 '18 at 10:29
  • @jpok in this [answer](https://stackoverflow.com/a/44685331/9695678) also recommend to use UUID to generate your primary key, but if you will read comments you can find that most of them also suggest to change `int` into `Integer` so your `foreignKey` will be `Nullable` – Asset Bekbossynov Dec 12 '18 at 10:36