# Cannot reference KeyPath correctly

**URL:** <https://forums.swift.org/t/cannot-reference-keypath-correctly/29820>\
**Category:** Using Swift\
**Tags:** vapor-3\
**Created:** [October 14, 2019, 7:24pm UTC](https://forums.swift.org/t/cannot-reference-keypath-correctly/29820 "2019-10-14T19:24:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonny](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jonny/32/7075_2.png) [@jonny](https://forums.swift.org/u/jonny)\
**Post date:** [October 14, 2019, 7:24pm UTC](https://forums.swift.org/t/cannot-reference-keypath-correctly/29820/1 "2019-10-14T19:24:34Z")

</div>

Hi

I'm having a little difficulty trying to use this key path. So wondering if someone can help point me in the right direction. I'm basically using a protocol to see whether the resource requested is able to be accessed by the user in a REST API.

**OrganizationRepresentable**

```swift
protocol OrganizationRepresentable: Model {
    typealias OrganizationKey = ReferenceWritableKeyPath<Self, UUID>
    static var organizationKey: OrganizationKey { get } // basically a reference to the organization FK on another model
}

extension OrganizationRepresentable where Self: Model {
    static func isBelongingToOrganization(user: User, on connection: DatabaseConnectable) throws -> EventLoopFuture<Int>
    {
        let userId = try user.requireID()
// checks a user can access this resource 
        return UserOrganization
                .query(on: connection)
                .filter(\.userId == userId)
                .filter(\UserOrganization.organizationId == Self.organizationKey) --> Cannot convert value of type 'Bool' to expected argument type 'FilterOperator<_, _>'
                .count()
    }
}

```

So, the filter() above, I feel like I've tried almost every possible way to reference the keyPath, but I'm obviously missing something. For completeness, this is how I conform the model `Campaign` and the extension on the request.

```swift
final class Campaign: Codable {
    var id: UUID?
    var organizationId: Organization.ID
    ...
}
extension Campaign: PostgreSQLUUIDModel {}
extension Campaign: Content {}
extension Campaign: Parameter {}

extension Campaign: OrganizationRepresentable {
    static var organizationKey: OrganizationKey = \Campaign.organizationId
}

// extension on request to 
extension Request {
    func isOrganizationRepresentable<O>(resource: O.Type, user: User, on connection: Request) throws -> EventLoopFuture<Bool> where O: OrganizationRepresentable
    {
        return try resource.isBelongingToOrganization(user: user, on: connection).map { total in
            return total > 0
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [October 14, 2019, 7:32pm UTC](https://forums.swift.org/t/cannot-reference-keypath-correctly/29820/2 "2019-10-14T19:32:35Z")

</div>

> [@jonny](#):
>
> `.filter(\.userId == userId)`

This is not supported as of now (even after [SE-0249](https://github.com/apple/swift-evolution/blob/master/proposals/0249-key-path-literal-function-expressions.md)).

---

<div class="post-metadata">

**Author:** ![jonny](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jonny/32/7075_2.png) [@jonny](https://forums.swift.org/u/jonny)\
**Post date:** [October 14, 2019, 7:38pm UTC](https://forums.swift.org/t/cannot-reference-keypath-correctly/29820/3 "2019-10-14T19:38:17Z")

</div>

This line works fine, it's Vapor's `filter()` for its DB layer Fluent.  
It's the line below that I can't get to compile, which also uses Fluent

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [October 14, 2019, 8:05pm UTC](https://forums.swift.org/t/cannot-reference-keypath-correctly/29820/4 "2019-10-14T20:05:58Z")

</div>

Oh, I thought it was the default `filter` from stdlib!

---

<div class="post-metadata">

**Author:** ![DeFrenZ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/defrenz/32/827_2.png) [@DeFrenZ](https://forums.swift.org/u/DeFrenZ)\
**Post date:** [October 15, 2019, 11:08am UTC](https://forums.swift.org/t/cannot-reference-keypath-correctly/29820/5 "2019-10-15T11:08:14Z")

</div>

> [@jonny](#):
>
> It's the line below that I can't get to compile

Then if I had to guess the issue is that you do `Self.`. You're in a `static` context, accessing a `static var`.
