alexk
alexk

Reputation: 213

How to make a complex query in DBFlow?

I've the following models :

class Car {

    @Column(columnType = Column.PRIMARY_KEY_AUTO_INCREMENT, name = "id")
    long id;

    @Column(columnType = Column.FOREIGN_KEY, references = {@ForeignKeyReference(columnName = "engine", columnType = String.class,  foreignColumnName = "id")})
    Engine engine;

    // .. other columns
}


class Engine {
    @Column(columnType = Column.PRIMARY_KEY_AUTO_INCREMENT, name = "id")
    long id;


    // .. other columns
}

I would like to query for a Car that have a specific engine ID.

Engine engine = new Select().from(Engine.class).where(Condition.column(Engine$Table.ID).is(engineId).querySingle();
Car car = new Select().from(Car.class).where(Condition.column(Engine$Table.ENGINE).is(engine).querySingle();

Although engine is found, the Car returns null

Upvotes: 3

Views: 2441

Answers (1)

Peter P
Peter P

Reputation: 501

Since the engine is defined as a foreign key within car you can just simply use a query that looks as follows.

new Select().from(Car.class)
.where(Condition.column(Car$Table.ENGINE_ENGINE)
.is([specific_engine_id])
.querySingle();

Upvotes: 3

Related Questions