-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Comments
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... |
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. |
I think using the unique annotation on that column will work? I could be wrong |
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. @Override
public long getItemId(int position) {
T mydata = this.getItem(position);
return mydata.mykeycolumn;
} in your Adapters for example. |
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 |
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? |
@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;
} |
Following @SeanPONeil 's implementation I tend to get |
Is it possible, or on the road map to have a custom field as the primary key?
The text was updated successfully, but these errors were encountered: