{"action":"opened","issue":{"url":"https://api.github.com/repos/realm/realm-java/issues/2167","labels_url":"https://api.github.com/repos/realm/realm-java/issues/2167/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-java/issues/2167/comments","events_url":"https://api.github.com/repos/realm/realm-java/issues/2167/events","html_url":"https://github.com/realm/realm-java/issues/2167","id":129016368,"number":2167,"title":"Add primary key on migration","user":{"login":"rshah","id":303216,"avatar_url":"https://avatars.githubusercontent.com/u/303216?v=3","gravatar_id":"","url":"https://api.github.com/users/rshah","html_url":"https://github.com/rshah","followers_url":"https://api.github.com/users/rshah/followers","following_url":"https://api.github.com/users/rshah/following{/other_user}","gists_url":"https://api.github.com/users/rshah/gists{/gist_id}","starred_url":"https://api.github.com/users/rshah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rshah/subscriptions","organizations_url":"https://api.github.com/users/rshah/orgs","repos_url":"https://api.github.com/users/rshah/repos","events_url":"https://api.github.com/users/rshah/events{/privacy}","received_events_url":"https://api.github.com/users/rshah/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-01-27T03:19:57Z","updated_at":"2016-01-27T03:19:57Z","closed_at":null,"body":"I want to add new field as primary key to old model (there was no primary key in my model but the contents are unique), \r\nbut in migration i got this error:\r\n```Java\r\njava.lang.IllegalArgumentException: Illegal Argument: Field \"projectUniqueKey\" cannot be a primary key, it already contains duplicate values: \r\n```\r\nThis is the new model\r\n```Java\r\nclass Project extends RealmObject {\r\n   @PrimaryKey @Index\r\n   private String projectUniqueKey;\r\n   ...\r\n}\r\n```\r\nThis is my migration code\r\n```Java\r\nRealmObjectSchema projectSchema = schema.get(\"Project\");\r\n        projectSchema.addField(\"projectUniqueKey\", String.class, FieldAttribute.PRIMARY_KEY)\r\n                .transform(new RealmObjectSchema.Function() {\r\n                    @Override\r\n                    public void apply(DynamicRealmObject obj) {\r\n                        obj.set(\"projectUniqueKey\", getUniqueKey());\r\n                    }\r\n                });\r\n```\r\nI'm using 0.87.3"}}