Eliott Roynette
Eliott Roynette

Reputation: 734

How to use a different foreign key in ORMLite?

I have a problem with a foreign key in ORMLite I have 2 classes QuestionDb and ResponsesDb which are the following :

public class ResponsesDb {

    public static final String FIELD_ID = "id";

    @DatabaseField(generatedId = true,columnName=FIELD_ID)
    private int id;
    @DatabaseField(canBeNull = false, foreign = true, foreignColumnName=QuestionDb.FIELD_REF)
    private QuestionDb question;
    @DatabaseField(canBeNull = false)
    private String answer;
 }

And :

@DatabaseTable(tableName = "question")
public class QuestionDb implements Serializable {

    public static final String FIELD_REF = "ref";

    private static final long serialVersionUID = 4106020204304605623L;


    @DatabaseField(generatedId = true)
    private int id;
    @DatabaseField(canBeNull = false, unique = true, columnName=FIELD_REF, index=true)
    private String ref;
    @ForeignCollectionField(foreignFieldName = "question", eager = true)
    private ForeignCollection<ResponsesDb> responses;
}

My problem is when i do that :

    QueryBuilder<QuestionDb, Integer> questionQuery = helper
            .getQuestionDao().queryBuilder();
    QueryBuilder<ResponsesDb, Integer> responseQuery = helper
            .getResponseDao().queryBuilder();
    responseQuery = responseQuery.join(questionQuery);

I recieve that :

05-27 12:00:01.577: W/System.err(7272): java.sql.SQLException: Could not find a foreign class model.ormlite.tableClass.ResponsesDb field in class model.ormlite.tableClass.QuestionDb or vice versa

But if I remove the field foreignColumnName=QuestionDb.FIELD_REF from question field's annotation in ResponsesDb, the query works.

The fact is that as my program update the database, the id field can change so I want that the foreign key is ref. Do you have any idea how I can fix this problem ?

Upvotes: 1

Views: 4101

Answers (1)

matiash
matiash

Reputation: 55350

You can use a string as a foreign key. What you cannot do is define a foreign key with a foreignColumnName that is not the key of the other object.

From the example in the ORMLite documentation:

With foreign objects, just the id field from the Account is persisted to the Order table as the column "account_id".

In this case, you have in QuestionDb:

@DatabaseField(generatedId = true)
private int id;

The generatedId annotation means that this is the id of the table. Having marked ref as unique will create a unique index, but it does not make it a candidate key. Thus it cannot be used as a foreign key from another table.

In short: if you need a string foreign key, then you can. Just define a string primary key in the referenced table. (i.e. remove the id field and put @DatabaseField(id = true) in the ref field.

Going further back, howerver, I don't understand why you claim:

The fact is that as my program update the database, the id field can change

The generatedId value will not change for a row after inserting it. It's perfect for use as a foreign key! :)

Upvotes: 2

Related Questions