mathesar-foundation / mathesar

Web application providing an intuitive user experience to databases.
https://mathesar.org/
GNU General Public License v3.0
2.28k stars 316 forks source link

UserDatabaseRoleMap error #3642

Closed seancolsen closed 6 days ago

seancolsen commented 1 week ago

I'm experiencing a bug which I've pinpointed to #3632.

On 2bf84ad352e541c60960a58c607e5b9a0e9c5e10 (immediately before that PR was merged into develop), I cannot reproduce the bug.

On 531d7dba411523ed3e7332e723710b67f472b819 (when that PR was merged into develop) I'm reproducing the bug as follows:

  1. Send an RPC request with the following body:

    {
      "jsonrpc": "2.0",
      "method": "schemas.list",
      "id": 0,
      "params": {
        "database_id": 1
      }
    }
  2. Expect a list of schemas.

  3. Observe:

    {
      "id": 0,
      "jsonrpc": "2.0",
      "error": {
        "code": -28009,
        "message": "DoesNotExist: UserDatabaseRoleMap matching query does not exist."
      }
    }

I see the following error output from the service container:

mathesar_service_dev  | [25/Jun/2024 18:23:55] "POST /api/rpc/v0/ HTTP/1.1" 200 133
mathesar_service_dev  | [-28009] DoesNotExist: UserDatabaseRoleMap matching query does not exist.
mathesar_service_dev  | Traceback (most recent call last):
mathesar_service_dev  |   File "/code/mathesar/rpc/exceptions/handlers.py", line 16, in safe_func
mathesar_service_dev  |     return f(*args, **kwargs)
mathesar_service_dev  |   File "/code/mathesar/rpc/schemas.py", line 76, in list_
mathesar_service_dev  |     with connect(database_id, user) as conn:
mathesar_service_dev  |   File "/code/mathesar/rpc/utils.py", line 12, in connect
mathesar_service_dev  |     user_database_role = UserDatabaseRoleMap.objects.get(
mathesar_service_dev  |   File "/usr/local/lib/python3.9/site-packages/django/db/models/manager.py", line 87, in manager_method
mathesar_service_dev  |     return getattr(self.get_queryset(), name)(*args, **kwargs)
mathesar_service_dev  |   File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 637, in get
mathesar_service_dev  |     raise self.model.DoesNotExist(
mathesar_service_dev  | mathesar.models.base.UserDatabaseRoleMap.DoesNotExist: UserDatabaseRoleMap matching query does not exist.
mathesar_service_dev  | 
mathesar_service_dev  | During handling of the above exception, another exception occurred:
mathesar_service_dev  | 
mathesar_service_dev  | Traceback (most recent call last):
mathesar_service_dev  |   File "/usr/local/lib/python3.9/site-packages/modernrpc/handlers/jsonhandler.py", line 182, in process_single_request
mathesar_service_dev  |     result_data = _method.execute(context, args, kwargs)
mathesar_service_dev  |   File "/usr/local/lib/python3.9/site-packages/modernrpc/core.py", line 143, in execute
mathesar_service_dev  |     return self.function(*args, **kwargs)
mathesar_service_dev  |   File "/code/mathesar/rpc/exceptions/handlers.py", line 18, in safe_func
mathesar_service_dev  |     _raise_generic_error(e)
mathesar_service_dev  |   File "/code/mathesar/rpc/exceptions/handlers.py", line 25, in _raise_generic_error
mathesar_service_dev  |     raise RPCException(err_code, e.__class__.__name__ + ": " + str(e))
mathesar_service_dev  | modernrpc.exceptions.RPCException: [-28009] DoesNotExist: UserDatabaseRoleMap matching query does not exist.

I've restarted my Docker container. I've also tried manually running migrations via:

docker exec mathesar_service_dev sh -c "python manage.py migrate"

@mathemancer can you help me troubleshoot this or fix it?

Anish9901 commented 1 week ago

@seancolsen you'd have to register your user by hitting this endpoint which will create an entry for the UserDatabaseRoleMap and should resolve this issue. https://github.com/mathesar-foundation/mathesar/blob/36bea27616477f3ff754cb8ec411ddf1638c1ab0/mathesar/rpc/connections.py#L130-L146

seancolsen commented 6 days ago

Thanks @Anish9901! That makes sense.

For anyone else (including future-me) I fixed this by sending the following RPC request:

{
  "jsonrpc": "2.0",
  "method": "connections.grant_access_to_user",
  "id": 0,
  "params": {
    "connection_id": 1,
    "user_id": 1
  }
}