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

[3.0.0-beta2] Index/IndexGroup not working #578

Closed brwskitime closed 8 years ago

brwskitime commented 8 years ago

I've followed the instructions. Here's the relevant part of my table showing what I want to index:

@Table(
        database = SNOCRUDatabase.class,
        name = ChatterConversation.TABLE_NAME,
        indexGroups = {@IndexGroup(number = 1, name = "serverOrder")})
public class ChatterConversation extends BaseTable implements Serializable {

    @Index(indexGroups = 1)
    @Column long serverOrder = 0;

}

And then a query to get rows ordered by the index:

    @Override
    public List<ChatterConversation> getChatterConversationList() {
        chatterGateway.getChatterConversations(new GetChatterConversationsListResponseCallback());
        return SQLite.select()
                .from(ChatterConversation.class)
                .indexedBy(ChatterConversation_Table.index_serverOrder)
                .orderBy(ChatterConversation_Table.serverOrder, true)
                .queryList();
    }

Pretty straight forward and it all compiles. However pulling the database from my emulator shows that no index was created and of course I get an exception as soon as I run the query.

java.lang.RuntimeException: Unable to resume activity {com.phonegap.snocru.review/com.snocru.snocru.MainActivity}: android.database.sqlite.SQLiteException: no such index: serverOrder (code 1): , while compiling: SELECT * FROM `chatter_conversation`INDEXED BY `serverOrder` ORDER BY `serverOrder` ASC

If I yank the .indexedBy() call, It'll run but there still is no index. Yes I've tried making the index name something other than the column name. What am I missing?

relevante commented 8 years ago

I don't think index creation works automatically. At least in my experience I've had to run a migration to get it to work. There's a thread at https://github.com/Raizlabs/DBFlow/issues/160 regarding doing this on initial db creation:

agrosner commented 8 years ago

it will do a CREATE IF NOT EXISTS statement if you execute it. Also there's an IndexMigration which you can use to auto-add Index to DB for versions. But I think this can be better via the generated IndexProperty. I will look into making a simple one for that.

agrosner commented 8 years ago

just create a subclass of BaseMigration like so:

public class IndexPropertyMigration extends BaseMigration {

    private final IndexProperty indexProperty;

    public IndexPropertyMigration(IndexProperty indexProperty) {
        this.indexProperty = indexProperty;
    }

    @Override
    public void migrate(DatabaseWrapper database) {
        indexProperty.createIfNotExists(database);
    }
}

and subclass it with proper annotation:

@Migration(version = MyDatabase.VERSION, database = MyDatabase.class)
public class MyIndexPropertyMigration extends IndexPropertyMigration {

    private final IndexProperty indexProperty;

    public IndexPropertyMigration() {
        super(MyTable_Table.index_myIndexProperty);
    }
}
brwskitime commented 8 years ago

Why do you need a migration on a new table?? It should be part of the table creation. It's also not consistent with unique index creation. Also tried this and it only runs the migration on a database upgrade. So new users/fresh install will still not get my index. This is broken IMO.

brucexia commented 8 years ago

Any updates on this? Why shall this not be fixed for fresh installs?

Amejia481 commented 6 years ago

I'm completely agree with @brwskitime :+1: Any updates on this?