Closed lingxiaoyang closed 4 years ago
Thanks for reporting and your very detailed bug report. :+1:
Deleting with an empty list actually has never been implemented (see #244) but it seems as a side product of #242 it is now worse. And when quickly looking at the code your first issue also makes sense why it occurs but it should not...
RelationshipView could need some love... pull request are very welcome.
Hi folks, I think I run into two unexpected behaviors with RelationshipView's PATCH endpoint when the relationship is to-many. I'm fairly new on JSON:API, so in case I'm doing anything wrong, please let me know!
The 1st issue is that, the endpoint throws an IntegrityError when PATCHing the endpoint with the same object as retrieved.
The 2nd issue is that, the endpoint incorrectly deletes the actual related object when PATCHing with an empty list, while I only expect it to delete the relationship from the m2m table.
The POST and DELETE endpoints were working as expected.
Steps to replicate
I was able to replicate it with a minimal example https://github.com/lingxiaoyang/dja-patch-issue on Python 3.8 and the latest stable Django, DRF and both stable and master DJA.
To replicate, please clone my test repo and run:
In the shell, create the base objects:
Proof
``` $ docker-compose run web ./manage.py shell Python 3.8.2 (default, Apr 16 2020, 18:25:46) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from dja_patch_issue.models import Article, Tag >>> Article.objects.create()The endpoint to test is either:
http://localhost:8000/articles/1/relationships/tags/ or
http://localhost:8000/tags/1/relationships/articles/
First, GET the relationship endpoint and verify the data is empty
``` $ curl -i -X GET http://localhost:8000/articles/1/relationships/tags/ HTTP/1.1 200 OK Date: Tue, 21 Apr 2020 14:06:36 GMT Server: WSGIServer/0.2 CPython/3.8.2 Content-Type: application/vnd.api+json Vary: Accept, Cookie Allow: GET, POST, PATCH, DELETE, HEAD, OPTIONS X-Frame-Options: DENY Content-Length: 11 X-Content-Type-Options: nosniff {"data":[]} ```Then, PATCH the relationship endpoint to add a relationship
``` $ curl -i -X PATCH -H "Content-Type: application/vnd.api+json" -d '{"data":[{"type": "tag", "id": "1"}]}' http://localhost:8000/articles/1/relationships/tags/ HTTP/1.1 200 OK Date: Tue, 21 Apr 2020 14:08:22 GMT Server: WSGIServer/0.2 CPython/3.8.2 Content-Type: application/vnd.api+json Vary: Accept, Cookie Allow: GET, POST, PATCH, DELETE, HEAD, OPTIONS X-Frame-Options: DENY Content-Length: 34 X-Content-Type-Options: nosniff {"data":[{"type":"tag","id":"1"}]} ```Now, PATCH the relationship endpoint again with same data. The 500 IntegrityError is returned while it should make no changes to the existing relationship.
``` $ curl -i -X PATCH -H "Content-Type: application/vnd.api+json" -d '{"data":[{"type": "tag", "id": "1"}]}' http://localhost:8000/articles/1/relationships/tags/ HTTP/1.1 500 Internal Server Error Date: Tue, 21 Apr 2020 14:08:45 GMT Server: WSGIServer/0.2 CPython/3.8.2 Content-Type: text/plain; charset=utf-8 X-Frame-Options: DENY Content-Length: 14870 Vary: Cookie X-Content-Type-Options: nosniff IntegrityError at /articles/1/relationships/tags/ FOREIGN KEY constraint failed Request Method: PATCH Request URL: http://localhost:8000/articles/1/relationships/tags/ Django Version: 3.0.5 Python Executable: /usr/local/bin/python Python Version: 3.8.2 Python Path: ['/code', '/usr/local/lib/python38.zip', '/usr/local/lib/python3.8', '/usr/local/lib/python3.8/lib-dynload', '/usr/local/lib/python3.8/site-packages'] Server time: Tue, 21 Apr 2020 14:08:45 +0000 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'dja_patch_issue'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware'] Traceback (most recent call last): File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 243, in _commit return self.connection.commit() The above exception (FOREIGN KEY constraint failed) was the direct cause of the following exception: File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/usr/local/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "/usr/local/lib/python3.8/site-packages/django/views/generic/base.py", line 71, in view return self.dispatch(request, *args, **kwargs) File "/usr/local/lib/python3.8/site-packages/rest_framework/views.py", line 505, in dispatch response = self.handle_exception(exc) File "/usr/local/lib/python3.8/site-packages/rest_framework/views.py", line 465, in handle_exception self.raise_uncaught_exception(exc) File "/usr/local/lib/python3.8/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception raise exc File "/usr/local/lib/python3.8/site-packages/rest_framework/views.py", line 502, in dispatch response = handler(request, *args, **kwargs) File "/usr/local/lib/python3.8/site-packages/rest_framework_json_api/views.py", line 319, in patch related_instance_or_manager.add(*serializer.validated_data) File "/usr/local/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 951, in add self._add_items( File "/usr/local/lib/python3.8/site-packages/django/db/transaction.py", line 232, in __exit__ connection.commit() File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 267, in commit self._commit() File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 243, in _commit return self.connection.commit() File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 243, in _commit return self.connection.commit() Exception Type: IntegrityError at /articles/1/relationships/tags/ Exception Value: FOREIGN KEY constraint failed Request information: USER: AnonymousUser GET: No GET data POST: No POST data FILES: No FILES data COOKIES: No cookie data META: CONTENT_LENGTH = '37' CONTENT_TYPE = 'application/vnd.api+json' DJANGO_SETTINGS_MODULE = 'dja_patch_issue.settings' GATEWAY_INTERFACE = 'CGI/1.1' GPG_KEY = 'E3FF2839C048B25C084DEBE9B26995E310250568' HOME = '/root' HOSTNAME = '19fa2b1473ac' HTTP_ACCEPT = '*/*' HTTP_HOST = 'localhost:8000' HTTP_USER_AGENT = 'curl/7.64.1' LANG = 'C.UTF-8' PATH = '/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' PATH_INFO = '/articles/1/relationships/tags/' PYTHONUNBUFFERED = '1' PYTHON_GET_PIP_SHA256 = '421ac1d44c0cf9730a088e337867d974b91bdce4ea2636099275071878cc189e' PYTHON_GET_PIP_URL = 'https://github.com/pypa/get-pip/raw/d59197a3c169cef378a22428a3fa99d33e080a5d/get-pip.py' PYTHON_PIP_VERSION = '20.0.2' PYTHON_VERSION = '3.8.2' QUERY_STRING = '' REMOTE_ADDR = '172.26.0.1' REMOTE_HOST = '' REQUEST_METHOD = 'PATCH' RUN_MAIN = 'true' SCRIPT_NAME = '' SERVER_NAME = '19fa2b1473ac' SERVER_PORT = '8000' SERVER_PROTOCOL = 'HTTP/1.1' SERVER_SOFTWARE = 'WSGIServer/0.2' TZ = 'UTC' wsgi.errors = <_io.TextIOWrapper name='Then, PATCH the endpoint with empty data. It says the relationship is cleared.
``` $ curl -i -X PATCH -H "Content-Type: application/vnd.api+json" -d '{"data":[]}' http://localhost:8000/articles/1/relationships/tags/ HTTP/1.1 200 OK Date: Tue, 21 Apr 2020 14:10:16 GMT Server: WSGIServer/0.2 CPython/3.8.2 Content-Type: application/vnd.api+json Vary: Accept, Cookie Allow: GET, POST, PATCH, DELETE, HEAD, OPTIONS X-Frame-Options: DENY Content-Length: 11 X-Content-Type-Options: nosniff {"data":[]} ```However, the related object itself was just incorrectly deleted. The database query returns nothing.
``` $ docker-compose run web ./manage.py shell Python 3.8.2 (default, Apr 16 2020, 18:25:46) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from dja_patch_issue.models import Article, Tag >>> Tag.objects.all()