Repository: dsd-dbs/capella-collab-manager
Issue: Error when deleting a model from a project

I removed all model sources from a model, and now want to delete the model from the project. However, I'm getting an error with the following stack trace:

```
time="2023-06-23 10:07:09,231" level=DEBUG function=dispatch client="10.128.67.53:51244" trace_id="OLI8C0H33O" method="DELETE" path="/api/v1/projects/<project>/models/<model>" user="<me>" message="request started"
time="2023-06-23 10:07:09,269" level=ERROR function=dispatch client="10.128.67.53:51244" trace_id="OLI8C0H33O" method="DELETE" path="/api/v1/projects/<project>/models/<model>" user="<me>" message="(psycopg2.errors.NotNullViolation) null value in column "model_id" of relation "model_restrictions" violates not-null constraint
DETAIL: Failing row contains (66, null, f).
[SQL: UPDATE model_restrictions SET model_id=%(model_id)s WHERE model_restrictions.id = %(model_restrictions_id)s]
[parameters: {'model_id': None, 'model_restrictions_id': 66}]
(Background on this error at: https://sqlalche.me/e/20/gkpj)"
Traceback (most recent call last):
File "/tmp/backend/capellacollab/core/logging.py", line 75, in dispatch
  return await call_next(request)
         ^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/base.py", line 84, in call_next
  raise app_exc
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/base.py", line 70, in coro
  await self.app(scope, receive_or_disconnect, send_no_error)
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/base.py", line 108, in __call__
  response = await self.dispatch_func(request, call_next)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/backend/capellacollab/core/logging.py", line 86, in dispatch
  response: fastapi.Response = await call_next(request)
                               ^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/base.py", line 84, in call_next
  raise app_exc
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/base.py", line 70, in coro
  await self.app(scope, receive_or_disconnect, send_no_error)
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/base.py", line 108, in __call__
  response = await self.dispatch_func(request, call_next)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/starlette_prometheus/middleware.py", line 57, in dispatch
  raise e from None
File "/usr/local/lib/python3.11/site-packages/starlette_prometheus/middleware.py", line 53, in dispatch
  response = await call_next(request)
             ^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/base.py", line 84, in call_next
  raise app_exc
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/base.py", line 70, in coro
  await self.app(scope, receive_or_disconnect, send_no_error)
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
  raise exc
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
  await self.app(scope, receive, sender)
File "/usr/local/lib/python3.11/contextlib.py", line 222, in __aexit__
  await self.gen.athrow(typ, value, traceback)
File "/usr/local/lib/python3.11/site-packages/fastapi/concurrency.py", line 36, in contextmanager_in_threadpool
  raise e
sqlalchemy.exc.IntegrityError: (psycopg2.errors.NotNullViolation) null value in column "model_id" of relation "model_restrictions" violates not-null constraint
DETAIL: Failing row contains (66, null, f).
[SQL: UPDATE model_restrictions SET model_id=%(model_id)s WHERE model_restrictions.id = %(model_restrictions_id)s]
[parameters: {'model_id': None, 'model_restrictions_id': 66}]
(Background on this error at: https://sqlalche.me/e/20/gkpj)
```

Changed files:
backend/capellacollab/projects/toolmodels/models.py

diff --git a/backend/capellacollab/projects/toolmodels/models.py b/backend/capellacollab/projects/toolmodels/models.py
index c9042b5c..0e32e0c5 100644
--- a/backend/capellacollab/projects/toolmodels/models.py
+++ b/backend/capellacollab/projects/toolmodels/models.py
@@ -99,7 +99,7 @@ class DatabaseCapellaModel(database.Base):
     )
 
     restrictions: orm.Mapped[DatabaseToolModelRestrictions] = orm.relationship(
-        back_populates="model", uselist=False
+        back_populates="model", uselist=False, cascade="delete"
     )