Search code examples
androidstackmob

How to update a schema using Stackmob?


Firstly I want to say, I really liked Stackmob.

But I've got some little problems because I'm a newbie on stackmob.

  1. I'm developing on Android sdk
  2. I have created a schema called "level" and it has 2 unique indexes (facebook_id and level_no)

My question is how to insert, update and delete (crud) the rows by facebook_id and level_no.

(ps: I can update a schema if it has 1 unique index but when index counts are greater than 1, I dont know how to do it.)


Solution

  • An index isn't like a primary key; it doesn't enforce uniqueness, it just speeds up querying on those fields. You still have to think in terms of level_id as your primary key. It's not hard to do CRUD operations in terms of other fields though. For insert, if you leave out the primary key, one will be generated for you. For the other operations, you can query by the field you want:

    Level.query(Level.class, new StackMobQuery().fieldIsEqualTo("facebook_id", "foo"), new StackMobQueryCallback<Level>() {...});
    

    then once you've got your Level object, simply resave or delete

    myLevel.setSomething("bar");
    myLevel.save();
    // or
    myLevel.delete();
    

    If you're using the datastore api, it's the same idea, you're just making the REST API calls directly.