Open samdze opened 1 year ago
same problem
This problem may be caused by the func SiblingsProperty.detach(_:on:)
.
public func detach(_ tos: [To], on database: Database) -> EventLoopFuture<Void> {
guard let fromID = self.idValue else {...}
let toIDs = tos.map {...}
return Through.query(on: database)
.filter(self.from.appending(path: \.$id) == fromID)
.filter(self.to.appending(path: \.$id) ~~ toIDs)
.delete()
}
The last line which calls .delete()
might be the reason why the issue happened.
The .delete()
just simply build a sql and runs it, but miss invoking other lifecycle, such as middleware.
To fix this, the simplest solution is changing the .delete()
to .delete(on:)
public func detach_try(_ tos: [To], on database: Database) async throws {
guard let fromID = self.fromId else {...}
let toIDs = tos.map {...}
try await Through.query(on: database)
.filter(self.from.appending(path: \.$id) == fromID)
.filter(self.to.appending(path: \.$id) ~~ toIDs)
.all().delete(on: database)
}
Describe the bug
When calling
detach
ordetachAll
on a SiblingsProperty, the related pivot model doesn't trigger its delete lifecycle events. Works fine with create events usingattach
.I tried and successfully reproduced the bug on SQLite and PostgreSQL, but it may be present in other drivers too.
To Reproduce
Steps to reproduce the behavior:
Create the following models:
final class Tag: Model, Content { static let schema = "tag"
}
final class TodoTagPivot: Model, Content { static let schema = "todo_tag"
}
func boot(routes: RoutesBuilder) throws { let todos = routes.grouped("todos") ... todos.group(":todoID") { todo in todo.patch(use: update) ... } }
extension Todo { struct Update: Content { let tagIDs: [UUID] } }
func update(req: Request) async throws -> Todo { let todoID = try req.parameters.require("todoID", as: UUID.self) let dto = try req.content.decode(Todo.Update.self)
}