Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom primary key? #22

Closed
josh-burton opened this issue Jan 5, 2013 · 8 comments
Closed

Custom primary key? #22

josh-burton opened this issue Jan 5, 2013 · 8 comments

Comments

@josh-burton
Copy link

Is it possible, or on the road map to have a custom field as the primary key?

@m2d2
Copy link

m2d2 commented Jan 6, 2013

Doesn't seem to support it just yet (or at least that's what I see on code). ActiveRecord on rails generally defaults to creating an ID field too. I'd suggest you create an indexed unique column in your table and run with that while leaving the ID field alone...

@josh-burton
Copy link
Author

Basically the reason I asked is I'm not quite sure how to do an update on a list of items if I can't set the primary key.

My items get fetched from a server, and after fetching, I want to dump them to a database, so I can use that data when there is no internet connection.

The problem is the items from the server don't have a primary key set, so they will be inserted again, creating duplicates.

All the items from the server have a unique ID already, so If i could just use that as a pimary key I would be able to update the items easily.

@SeanPONeil
Copy link
Contributor

I think using the unique annotation on that column will work? I could be wrong

@vonloxley
Copy link
Contributor

Yeah, you can have unique keys since #18.

ActiveAndroid will still use its own ID for operations, so you need to use your key in where()-clauses etc.
Try overwriting

        @Override
        public long getItemId(int position) {
                T mydata = this.getItem(position);
                return mydata.mykeycolumn;
        }

in your Adapters for example.

@SeanPONeil
Copy link
Contributor

To do an update with a unique column as your pseudo primary key, the annotation would look something like this:

 @Column(name = "nid", unique = true, onUniqueConflict = Column.ConflictAction.REPLACE)
    private int nid;

where nid is your unique ID from the server

@derek-pavao
Copy link

I was also wondering if you can define a custom primary key, but for a slightly different reason.

My application is also grabbing data from a remote api and storing it locally in SQLite and later will sync that data back to the remote server.

The suggestion above of defining a column as unique and using Column.ConflictAction.REPLACE would work for me, However, my problem is semantics of the naming. I want the id on the server to be called "id" and the id on the device to be called something like "clientId".

Is there a solution I'm not seeing, or am I just being too picky here?

@ivanjolic95
Copy link

@dotDeeka if I understood correctly, you could do something like this:

@Table(name = "Items", id = "clientId")
public class Item extends Model {
    @Column(name = "id")
    private long id;
}

@hdavidzhu
Copy link

Following @SeanPONeil 's implementation I tend to get android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787) at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants