我正在制作聊天后端,我需要包含两个用户的消息历史记录表。
是否有某种方法可以正确地做某事?
static func prepare(_ database: Database) throws {
try database.create("historys") { history in
history.id()
history.parent(User.self, optional: false)
history.parent(User.self, optional: false)
}
}
现在我收到多个 user_id 字段的错误。
最佳答案
在你准备的时候应该真的可以设置字段名;这将是一个有用的增强。
与此同时,您可以通过创建一个 int
字段来获得相同的效果。
static func prepare(_ database: Database) throws {
try database.create("historys") { history in
history.id()
history.int("sender_user_id", optional: false)
history.int("recipient_user_id", optional: false)
}
}
在您的模型中,您将拥有属性 senderUserId: Node
和 recipientUserId: Node
,并且您将初始化它们,例如senderUserId = try Node.extract("sender_user_id")
。
然后您可以在模型上使用以下便捷方法获取每个关系:
func sender() throws -> Parent<User> {
return try parent(senderUserId)
}
func recipient() throws -> Parent<User> {
return try parent(recipientUserId)
}
关于swift - 一个实体中的两个相同的 parent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41852726/