【问题标题】:Many-to-Many relations in Vapor 3 with FluentVapor 3 与 Fluent 中的多对多关系
【发布时间】:2018-05-11 09:14:03
【问题描述】:

我想知道如何使用 Fluent 和 FluentMySQL 在 Vapor 3 中创建多对多关系,如 Vapor 2 docs 中所述

遗憾的是,docs for Vapor 3 尚未更新,Pivot 协议的实现已更改。

这是我想要做的:我有两个课程,UserCommunity。一个CommunitymembersUsers 可以是多个Communitys 的成员。

目前,我的代码如下所示:

import Vapor
import FluentMySQL

final class Community: MySQLModel {
    var id: Int?
    //Community Attributes here
}

final class User: MySQLModel {
    var id: Int?
    //User Attributes here
}

extension Community {
    var members: Siblings<Community, User, Pivot<Community, User>> {
        return siblings()
    }
}

但是,这会导致以下编译器错误:

Cannot specialize non-generic type 'Pivot'Using 'Pivot' as a concrete type conforming to protocol 'Pivot' is not supported

我看到有一个名为ModifiablePivot 的协议扩展,但我不知道如何使用它,因为任何地方都没有文档或示例代码。

感谢任何帮助。提前致谢!

【问题讨论】:

    标签: mysql swift vapor


    【解决方案1】:

    我有同样的问题,两个答案对我来说都不够,所以我只是发布了对我有用的解决方案。

    // in CommunityUser.swift
    import Vapor
    import FluentMySQL
    
    
    final class CommunityUser: MySQLPivot {
    
        typealias Left = User
        typealias Right = Community
    
        static var leftIDKey: LeftIDKey = \.userID
        static var rightIDKey: RightIDKey = \.communityID
    
        var id: Int?
        var userID: Int
        var communityID: Int
    
        init(id: Int?, userID: Int, communityID: Int) {
            self.id = id
            self.userID = userID
            self.communityID = communityID
        }
    
    }
    
    // CommunityUser didn't conform to MySQLMigration
    // This simple line solves the problem
    extension CommunityUser: MySQLMigration { }
    

    对我来说CommunityUser 也需要在数据库中迁移。

    // in configure.swift
    migrations.add(model: CommunityUser.self, database: .mysql)
    

    【讨论】:

      【解决方案2】:

      Fluent 3 不再像 Fluent 2 中的 Pivot 那样提供默认枢轴。您应该做的是创建一个符合 Pivot 的类型。 FluentMySQL 中有一些帮助类型。

      final class CommunityUser: MySQLPivot {
          // implement the rest of the protocol requirements
          var communityID: Community.ID
          var userID: User.ID
      }
      

      然后使用CommunityUser 代替Pivot&lt;Community, User&gt;

      【讨论】:

        【解决方案3】:

        完整代码:

        import Vapor
        import FluentSQLite
        
        final class Community: SQLiteModel {
            var id: Int?
            //Community Attributes here
        }
        
        final class User: SQLiteModel {
            var id: Int?
            //User Attributes here
        }
        
        extension Community {
            var members: Siblings<Community, User, CommunityUser> {
                return siblings()
            }
        }
        
        final class CommunityUser: SQLitePivot {
            typealias Left = User
            typealias Right = Community
            static let leftIDKey: LeftIDKey = \.userID
            static let rightIDKey: RightIDKey = \.communityID
            var id: Int?
            var userID: Int
            var communityID: Int
        }
        

        【讨论】:

        • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation would greatly improve its long-term value 通过展示为什么这是一个很好的解决问题的方法,并将使其对未来有其他类似问题的读者更有用。请edit您的回答添加一些解释,包括您所做的假设。
        猜你喜欢
        • 2018-12-28
        • 1970-01-01
        • 2022-11-24
        • 2012-09-22
        • 2011-03-16
        • 2018-02-04
        • 2014-12-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多