agrosner / DBFlow

A blazing fast, powerful, and very simple ORM android database library that writes database code for you.
MIT License
4.87k stars 598 forks source link

getDatabase called recursively while migration #1536

Closed artemtkachenko closed 6 years ago

artemtkachenko commented 6 years ago

DBFlow Version: 3.1.1

Bug or Feature Request: Bug I suppose, or missed feature.

Description:

Hello, I found an exception: java.lang.IllegalStateException: getDatabase called recursively. It happens while Migration. The reason of it my data structure I believe:

` class EntityA { @PrimaryKey() @Column private long uid;

@Column
private String name;

List<EntityB> subentities;

@OneToMany(methods = {OneToMany.Method.ALL}, variableName = "subentities")
public List<EntityB> getSubentities() {
    if (subentities == null || subentities.isEmpty()) {
        subentities = SQLite.select()
                .from(EntityB.class)
                .where(EntityB_Table.entityAUid.eq(uid))
                .queryList();
    }
    return subentities;
}

}

class EntityB { @PrimaryKey(autoincrement = true) private long uid;

@Column
private long entityAUid;

@Column
private String name;

} `

And migration: ` class Migration extends BaseMigration { ... @Override public void migrate(DatabaseWrapper database) { EntityA *entityA = SQLite.select() .from(EntityA.class) .where(EntityA.uid.eq(%UID%)) .querySingle(database);

}

} `

Crash happens when Model_Adapter is trying to load the EntityA from cursor because in this method: public List<EntityB> getSubentities() {..} app is trying to create a new DatabaseWrapper instance, because it want to continue getSubentities() transaction, but database is in initializing state and an exception appears.

I did not find the way how to set the right DatabaseWrapper instance in this method. The only hotfix what I am seeing now is using: @OneToMany(methods = {OneToMany.Method.SAVE} or anything else except @OneToMany(methods = {OneToMany.Method.LOAD} or @OneToMany(methods = {OneToMany.Method.ALL}to avoid loading from cursor;

Please advise what I am doing wrong and help me to figure it out.

Read your docs: https://agrosner.gitbooks.io/dbflow/content/Relationships.html https://agrosner.gitbooks.io/dbflow/content/Migrations.html

But it did not help me. Maybe this issue is fixed in newest version. But I would like to continue using a 3+ version. Thanks.

My apologize for code formatting, did not find the way how to make it better.

artemtkachenko commented 6 years ago

My apologies guys, I was an idiot. SQLite.update() works well;