DBFlow icon indicating copy to clipboard operation
DBFlow copied to clipboard

ForeignKey | Define column name of referenced class

Open paraqles opened this issue 10 years ago • 27 comments

Setup:

  • DBFlow Version: 4.0.0-beta1
  • Target Platform: Android 23
  • Gradle/Maven: Gradle v2.10 Android Gradle Plugin v2.0.0
  • Build Tools: 24.0.3
  • Kotlin: 1.0.3 with kapt

Issue Kind: Question

Description:

I want to reference a Model from another Model (See below). The problem with the version below is, I get

Cannot find symbol method getFrom()
Cannot find symbol method getTo()
Cannot find symbol method getFrom()
Cannot find symbol method getTo()
@Table(database = MMHDatabase::class)
class Transaction(from: Account? = null, value: Float = 0.0f, to: Account? = null, id: Long = -1) : BaseModel() {
    companion object {
        fun get(id: Long) : Transaction? {
            return (select
                    from Transaction::class
                    where (Transaction_Table.id eq id)).result
        }
    }

    @PrimaryKey(autoincrement = true)
    var id: Long = -1

    @ForeignKey
    var from: Account? = null

    @Column
    var value: Float = 0.0f

    @ForeignKey
    var to: Account? = null
}

Corresponding generated code (Line 3 and 9):

 1public final void bindToInsertValues(ContentValues values, Transaction model) {
 2    if (model.getFrom() != null) {
 3      values.put("from_id", model.getFrom().getFrom());
 4    } else {
 5      values.putNull("from_id");
 6    }
 7    values.put("value", model.getValue());
 8    if (model.getTo() != null) {
 9      values.put("to_id", model.getTo().getTo());
10    } else {
11      values.putNull("to_id");
12    }
13  }

and (Line 3 and 9)

 1 public final void bindToInsertStatement(DatabaseStatement statement, Transaction model, int start) {
 2    if (model.getFrom() != null) {
 3      statement.bindLong(1 + start, model.getFrom().getFrom());
 4    } else {
 5      statement.bindNull(1 + start);
 6    }
 7    statement.bindDouble(2 + start, model.getValue());
 8    if (model.getTo() != null) {
 9      statement.bindLong(3 + start, model.getTo().getTo());
10    } else {
11      statement.bindNull(3 + start);
12    }
13  }

I expected it would use the get method of the primary key of the Account class which would be getId().

paraqles avatar Oct 24 '16 13:10 paraqles

I got it nearly working with:

@Table(database = MMHDatabase::class)
class Transaction(from: Account? = null, value: Float = 0.0f, to: Account? = null, id: Long = -1) : BaseModel() {
    companion object {
        fun get(id: Long) : Transaction? {
            return (select
                    from Transaction::class
                    where (Transaction_Table.id eq id)).result
        }
    }

    @PrimaryKey(autoincrement = true)
    var id: Long = -1

    @ForeignKey(
        references = arrayOf(
            ForeignKeyReference(
                columnName = "from_account_id",
                columnType = Long::class,
                foreignKeyColumnName = "id",
                referencedGetterName = "getId",
                referencedSetterName = "setId",
                referencedFieldIsPrivate = true
            )
        )
    )
    var from: Account? = null

    @Column
    var value: Float = 0.0f

    @ForeignKey (
        references = arrayOf(
            ForeignKeyReference(
                columnName = "to_account_id",
                columnType = Long::class,
                foreignKeyColumnName = "id",
                referencedGetterName = "getId",
                referencedSetterName = "setId",
                referencedFieldIsPrivate = true
            )
        )
    )
    var to: Account? = null
}

paraqles avatar Oct 24 '16 14:10 paraqles

same problem

  @Override
  public final void bindToInsertStatement(DatabaseStatement statement, Voce model, int start) {
    statement.bindLong(1 + start, model.getAnno());
    statement.bindDouble(2 + start, model.getValore());
    String refvaloreString = model.getValoreString();
    if (refvaloreString != null)  {
      statement.bindString(3 + start, refvaloreString);
    } else {
      statement.bindNull(3 + start);
    }
    if (model.getBanca() != null) {
      statement.bindLong(4 + start, model.getBanca().getBanca());
    } else {
      statement.bindNull(4 + start);
    }
    if (model.getIndicatore() != null) {
      statement.bindLong(5 + start, model.getIndicatore().getIndicatore());
    } else {
      statement.bindNull(5 + start);
    }
  }

but the correct code should be

 @Override
  public final void bindToInsertStatement(DatabaseStatement statement, Voce model, int start) {
    statement.bindLong(1 + start, model.getAnno());
    statement.bindDouble(2 + start, model.getValore());
    String refvaloreString = model.getValoreString();
    if (refvaloreString != null)  {
      statement.bindString(3 + start, refvaloreString);
    } else {
      statement.bindNull(3 + start);
    }
    if (model.getBanca() != null) {
      statement.bindLong(4 + start, model.getBanca().getId());
    } else {
      statement.bindNull(4 + start);
    }
    if (model.getIndicatore() != null) {
      statement.bindLong(5 + start, model.getIndicatore().getId());
    } else {
      statement.bindNull(5 + start);
    }
  }

in particular model.getBanca().getBanca()); should be model.getIndicatore().getId()

another info: im not using Kotlin, is very simple usage

xanscale avatar Oct 24 '16 14:10 xanscale

With the combination of referencedGetterName and referencedSetterName in conjunction with referencedFieldIsPrivate = true

I get:

@Override
  public final void bindToInsertValues(ContentValues values, Transaction model) {
    if (model.getFrom() != null) {
      values.put("from_account_id", model.getFrom().getId());
    } else {
      values.putNull("from_account_id");
    }
    values.put("value", model.getValue());
    if (model.getTo() != null) {
      values.put("to_account_id", model.getTo().getId());
    } else {
      values.putNull("to_account_id");
    }
  }

and

  @Override
  public final void bindToInsertStatement(DatabaseStatement statement, Transaction model, int start) {
    if (model.getFrom() != null) {
      statement.bindLong(1 + start, model.getFrom().getId());
    } else {
      statement.bindNull(1 + start);
    }
    statement.bindDouble(2 + start, model.getValue());
    if (model.getTo() != null) {
      statement.bindLong(3 + start, model.getTo().getId());
    } else {
      statement.bindNull(3 + start);
    }
  }

So that kind of is solved, it seems like there is no

This is no longer required as the library will auto-generate references for you based on the other table's primary keys.

  • (Master Branch) ForeignKey.java Line 16 and 17.

unfortunately I have now an 'infix' modifier is required on 'eq' in 'IntProperty' problem (But seems like a different error)

paraqles avatar Oct 24 '16 15:10 paraqles

can you post your model?

xanscale avatar Oct 24 '16 15:10 xanscale

@xanscale: please have a look at my second post.

paraqles avatar Oct 24 '16 15:10 paraqles

@paraqles i have very simple model

@Table(database = AppDatabase.class)
public class Voce extends BaseModel {

    @PrimaryKey(autoincrement = true)
    private long id;

    @ForeignKey
    private Banca banca;

    @ForeignKey
    private Indicatore indicatore;
}

how can i resolve?

xanscale avatar Oct 24 '16 15:10 xanscale

@Table(database = AppDatabase.class)
public class Voce extends BaseModel {

    @PrimaryKey(autoincrement = true)
    private long id;

    @ForeignKey(
        references = @ForeignKeyReference(
                columnName = "banca",
                columnType = Long.class,
                foreignKeyColumnName = "id",
                referencedGetterName = "getId",
                referencedSetterName = "setId",
                referencedFieldIsPrivate = true
            )
        )
    )
    private Banca banca;

    @ForeignKey(
        references = @ForeignKeyReference(
                columnName = "indicatore",
                columnType = Long.class,
                foreignKeyColumnName = "id",
                referencedGetterName = "getId",
                referencedSetterName = "setId",
                referencedFieldIsPrivate = true
            )
        )
    )
    private Indicatore indicatore;
}

Assuming Banca and Indicatore are also derived from BaseModel and thier ID field is id and is of type Long. Also there must be getter and setter called setId and getId.

paraqles avatar Oct 24 '16 15:10 paraqles

It is kind of ugly and not like it is stated in the comment of the corresponding code, but seems solved.

paraqles avatar Oct 24 '16 15:10 paraqles

i confirm that works. I'm hoping for an official response

xanscale avatar Oct 24 '16 15:10 xanscale

@paraqles i have found another workaround

public class Indicatore extends BaseModel {
   public Long getIndicatore() {return id;}
}

i don't know if is better or not :D

xanscale avatar Oct 24 '16 16:10 xanscale

Maybe, but in my example it would be:

class Account : BaseModel() {
    fun getFrom() : Int = id
}

that is totally not clear what is happening and what this function is for.

paraqles avatar Oct 26 '16 11:10 paraqles

@paraqles i think more like this. each model class must have a method that return the primarykey that is called getMODEL, like this:

class From : BaseModel() {
    fun getFrom() : Int = id
}

xanscale avatar Oct 26 '16 15:10 xanscale

@agrosner any opinion?

xanscale avatar Oct 26 '16 16:10 xanscale

@xanscale: you assume that the model I'm referencing is called From and not Account.

The misleading sentence here is more the one in ForeignKey.java

This is no longer required as the library will auto-generate references for you based on the other table's primary keys.

ForeignKeyReference[] references() default {};

com.raizlabs.android.dbflow.annotation.ForeignKey.java

From this sentence I expect that the processor uses reflection to find the @PrimaryKey of the referenced model and then uses the appropriate getter and not the reference holding variable name as getter on the referenced model. If the reflection should be avoided, it can be a convention, that a model has to implement the getId or getPrimaryKey method which is more obvious what is expected from the model.

paraqles avatar Oct 27 '16 07:10 paraqles

@paraqles so

class Account : BaseModel() {
    fun getAccount() : Int = id
}

i know that is a workaround and im waiting for an official response from @agrosner

xanscale avatar Oct 27 '16 09:10 xanscale

I think this applies to private fields. If you take away the private fields, this problem goes away?

rogerhu avatar Oct 30 '16 05:10 rogerhu

@rogerhu if i remove foreignKey and relative field problem goes away.

i think this is common and big problem. no one else has the same problem?

xanscale avatar Oct 30 '16 18:10 xanscale

Change your primary key columns to be public...does this issue go away?

rogerhu avatar Oct 30 '16 18:10 rogerhu

I had that problem few minutes ago and changing from private to public solved an issue

EDIT: it seems to me that Kotlin has that issue also. Fields in Kotlin are public by default but I'm getting an error

mister11 avatar Oct 30 '16 19:10 mister11

@rogerhu package access solve issue perfectly.

xanscale avatar Oct 31 '16 09:10 xanscale