question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ForeignKey | Define column name of referenced class

See original GitHub issue

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().

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:27 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
paraqlescommented, Oct 24, 2016
@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.

1reaction
agrosnercommented, Nov 2, 2016

found issue! fixed in develop. Sorry for no response (I was on vacation for a little while).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Foreign Key Constraint | CockroachDB Docs
The `FOREIGN KEY` constraint specifies a column can contain only values exactly matching existing values from the column it references.
Read more >
Mapping a foreign key with a custom column name
The Name value should be a comma separated list of foreign key property names. My entities are like this: [Table("WIDGETENTITIES")] public class WidgetEntity ......
Read more >
How to Create a Table with a Foreign Key in SQL
Discussion: Another way to define a foreign key during table creation is to use the FOREIGN KEY REFERENCES clause at the end of...
Read more >
13.1.20.5 FOREIGN KEY Constraints - MySQL :: Developer Zone
A foreign key relationship involves a parent table that holds the initial column values, and a child table with column values that reference...
Read more >
SQL FOREIGN KEY Constraint - W3Schools
The table with the foreign key is called the child table, and the table with the primary key is called the referenced or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found