{"action":"created","issue":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2996","labels_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2996/labels{/name}","comments_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2996/comments","events_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2996/events","html_url":"https://github.com/tomchristie/django-rest-framework/issues/2996","id":83748364,"number":2996,"title":"Unique constraint prevents nested serializers from updating","user":{"login":"xordoquy","id":2340654,"avatar_url":"https://avatars.githubusercontent.com/u/2340654?v=3","gravatar_id":"","url":"https://api.github.com/users/xordoquy","html_url":"https://github.com/xordoquy","followers_url":"https://api.github.com/users/xordoquy/followers","following_url":"https://api.github.com/users/xordoquy/following{/other_user}","gists_url":"https://api.github.com/users/xordoquy/gists{/gist_id}","starred_url":"https://api.github.com/users/xordoquy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xordoquy/subscriptions","organizations_url":"https://api.github.com/users/xordoquy/orgs","repos_url":"https://api.github.com/users/xordoquy/repos","events_url":"https://api.github.com/users/xordoquy/events{/privacy}","received_events_url":"https://api.github.com/users/xordoquy/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/labels/Bug","name":"Bug","color":"e10c02"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-06-01T23:45:10Z","updated_at":"2015-06-02T09:02:19Z","closed_at":null,"body":"If you have a nested serializer which model does have a unique constraint on a field you're not able to reference an existing instance.\r\n\r\n```\r\nclass Task(serializers.ModelSerializer):\r\n    class Meta:\r\n        model = models.Task\r\n        fields = ('id', 'name')\r\n\r\n\r\nclass Category(serializers.ModelSerializer):\r\n    tasks = Task(many=True)\r\n    class Meta:\r\n        model = models.Category\r\n        fields = ('id', 'name', 'tasks')\r\n```\r\n\r\nLet's say I created a category & task which I'm updating with the very same data they already have:\r\n\r\n```\r\nc = Category(data={\"id\": 1, \"name\": \"Django\", \"tasks\": [{\"id\": 1, \"name\": \"Demo\", \"owner\": \"admin\"}]})\r\nc.is_valid()  # Returns False\r\nc.errors  # Returns {'tasks': [{'name': ['This field must be unique.']}]}\r\n```\r\n\r\nThe point is, `name` remains unique because it matches its own `id` \r\n"},"comment":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/comments/107874848","html_url":"https://github.com/tomchristie/django-rest-framework/issues/2996#issuecomment-107874848","issue_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2996","id":107874848,"user":{"login":"xordoquy","id":2340654,"avatar_url":"https://avatars.githubusercontent.com/u/2340654?v=3","gravatar_id":"","url":"https://api.github.com/users/xordoquy","html_url":"https://github.com/xordoquy","followers_url":"https://api.github.com/users/xordoquy/followers","following_url":"https://api.github.com/users/xordoquy/following{/other_user}","gists_url":"https://api.github.com/users/xordoquy/gists{/gist_id}","starred_url":"https://api.github.com/users/xordoquy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xordoquy/subscriptions","organizations_url":"https://api.github.com/users/xordoquy/orgs","repos_url":"https://api.github.com/users/xordoquy/repos","events_url":"https://api.github.com/users/xordoquy/events{/privacy}","received_events_url":"https://api.github.com/users/xordoquy/received_events","type":"User","site_admin":false},"created_at":"2015-06-02T09:02:19Z","updated_at":"2015-06-02T09:02:19Z","body":"I'm a bit uncomfortable with this.\r\nLet's way our nested resource is either created or fetched from the DB ala `get_or_create`.\r\nIf we remove the validation, it may run in later issues.\r\nIf we keep it, it'll be impossible to get an item that already exists without changing it."}}