         }
         
         for join in rightQuery.joins {
-            sql += try " " + join.joinSQL(db, &arguments, leftQualifier: rightQualifier)
+            sql += try " " + join.joinSQL(db, &arguments, leftQualifier: rightQualifier, isRequiredAllowed: isRequiredAllowed)
         }
         
         return sql

        sql += try " FROM " + source.sourceSQL(db, &arguments)
        
        for join in joins {
            sql += try " " + join.joinSQL(db, &arguments, leftQualifier: qualifier!)
        }
        
        if let filter = try filterPromise.resolve(db) {

    var key: String
    var associationMapping: (Database) throws -> AssociationMapping
    
    func joinSQL(_ db: Database,_ arguments: inout StatementArguments?, leftQualifier: SQLTableQualifier, isRequiredAllowed: Bool) throws -> String {
        var isRequiredAllowed = isRequiredAllowed
        var sql = ""
        switch op {
        case .optional:
            isRequiredAllowed = false
            sql += "LEFT JOIN"
        case .required:
            guard isRequiredAllowed else {
                // TODO: chainOptionalRequired
                fatalError("Not implemented: chaining a required association behind an optional association")
            }
            sql += "JOIN"
        }
        
        let rightQualifier = rightQuery.qualifier!
