Error When Deleting User

I am creating a user, fetching a token, creating some acronyms, and deleting those acronyms. After which, I attempt to hard delete the user. However, this attempt fails with the following error:

[ ERROR ] PostgreSQLError.server.error.ri_ReportViolation: DELETE /api/users/A5FAE5EE-98D7-49CC-B9D9-FBAC85CC4962/force update or delete on table “User” violates foreign key constraint “fk:Token.userID+User.id” on table “Token” (ErrorMiddleware.swift:26)

[ DEBUG ] Possible causes for PostgreSQLError.server.error.ri_ReportViolation: Key (id)=(a5fae5ee-98d7-49cc-b9d9-fbac85cc4962) is still referenced from table “Token”. (ErrorMiddleware.swift:26)

I have tried deleting the user from the user’s account and the admin’s account, but receive the same errors. Am I missing something obvious?

Solved the problem by modifying the TILApp’s Token.swift. Specifically, I changed the following:

extension Token: Migration {
  static func prepare(on connection: PostgreSQLConnection) -> Future<Void> {
    return Database.create(self, on: connection) { builder in
      try addProperties(to: builder)
        builder.reference(from: \.userID, to: \User.id)
    }
  }
}

To the following:

extension Token: Migration {
  static func prepare(on connection: PostgreSQLConnection) -> Future<Void> {
    return Database.create(self, on: connection) { builder in
      try addProperties(to: builder)
        builder.reference(from: \.userID, to: \User.id, onDelete: .cascade)
    }
  }
}

This causes the app to delete any tokens associated with a user on the deletion of the user.

@gili yep that’s the right way and the way I would have suggested!