Yasin Kilicdere
Yasin Kilicdere

Reputation: 446

Why does DbFlow can not save objects without being assigned to a variable?

Code below works as expected

Customer x = new Customer();
x.name = "yasin";
x.save();

But this leads to an app crash

new Customer() {
  {
     name = "yasin";
  }
}.save();

Error detail in logcat:

com.raizlabs.android.dbflow.structure.InvalidDBConfiguration: 
Table: com.example.yasin.myapplication.MainActivityFragment$1$1 is not
registered with a Database. Did you forget the @Table annotation?

Why does this happen? Is it some bug with DbFlow or there is something I don't know about Java language?

Upvotes: 1

Views: 2844

Answers (2)

Neutron Stein
Neutron Stein

Reputation: 1965

The error you are getting is because in the second case you are extending Customer class using anonymous class and DBFlow require classes it manages be annotated, it is not the case for the anonymous class created. That is what leads to error. The solution would be add a constructor taking name parameter so you can do something like: new Customer("the name").save();

Upvotes: 3

rexxar
rexxar

Reputation: 1801

In your Customer class, did you put the @Table annotation to declare the database name in which the table is located? Database name is, per DBFlow tutorial, located in one of your DB related classes, in this example below, one called AppDatabase. (Version is needed for migrations)

@Database(name = AppDatabase.NAME, version = AppDatabase.VERSION)
public class AppDatabase {

    public static final String NAME = "MyDataBaseName";
    public static final int VERSION = 4;
}

Compare your entity class with this example

@Table(databaseName = AppDatabase.NAME)
public class TestModel extends BaseModel {

    // All tables must have a least one primary key
    @Column
    @PrimaryKey
    String name;

    // By default the column name is the field name
    @Column
    int randomNumber;
}

Taken from DBFlow Usage Readme on GitHub.

Upvotes: 1

Related Questions