# How to use Associations?

**URL:** <https://forums.swift.org/t/how-to-use-associations/50504>\
**Category:** GRDB\
**Created:** [July 17, 2021, 2:34pm UTC](https://forums.swift.org/t/how-to-use-associations/50504 "2021-07-17T14:34:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bzamayo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bzamayo/32/764_2.png) [@bzamayo](https://forums.swift.org/u/bzamayo)\
**Post date:** [July 17, 2021, 2:34pm UTC](https://forums.swift.org/t/how-to-use-associations/50504/1 "2021-07-17T14:34:11Z")

</div>

I'm trying to fetch a simple record with an association but I'm a little unclear about how the glue code works with the decoding of the QueryInterfaceRequest.

I have a request like

`let request = CalendarEntryRecord.filter(Column("id") == entryID).including(optional: CalendarEntryRecord.bookmark) `  
and defined a temporary struct to decode into (following the docs) like so

```swift
 struct Response : Equatable, FetchableRecord, Decodable {
    let entry: CalendarEntryRecord
    let bookmark: CalendarEntryBookmarkRecord?
}

```

However, when I perform a `Response.fetchAll(...` the bookmark property alway comes back as `nil`.  
I verified the table rows are present by using the `Row.fetchAll(...` form and printing the output:

```swift
[[id:Data(16 bytes) date:"2021-07-17 00:00:00.000" wordID:Data(16 bytes) wordDefaultForm:"noun" wordSpellingUS:"test"]
  unadapted: [id:Data(16 bytes) date:"2021-07-17 00:00:00.000" wordID:Data(16 bytes) wordDefaultForm:"noun" wordSpellingUS:"test"]" id:1 entryID:Data(16 bytes) createdAt:"2021-07-17 14:26:22.791"]
  - entry_bookmark: [id:1 entryID:Data(16 bytes) createdAt:"2021-07-17 14:26:22.791"]]`

```

I can see the `entry_bookmark` is in that print output with the expected values but it's not decoding into the `bookmark` property on the response, that is always `nil`.

What am I doing wrong? I feel like it must have something to do with the names of the properties on `Response` or involves customising `CodingKeys` somehow, but I'm not sure how/exactly what is awry.

Note: The print output is abridged to exclude a bunch more properties on the `CalendarEntryRecord` type.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [July 18, 2021, 8:18am UTC](https://forums.swift.org/t/how-to-use-associations/50504/2 "2021-07-18T08:18:12Z")

</div>

Hello @bzamayo,

Thanks for taking the time for displaying raw rows. We indeed see the `entry_bookmark` key that contains the columns for your bookmark record.

This `entry_bookmark` key is the _association key_ of the `CalendarEntryRecord.bookmark` association. All associations come with a key (which defaults to the table name of the associated record). It is those keys that allow decoding with the Decodable protocol, when they match property names (coding keys, precisely).

Your solution is thus to make sure the coding key/property name matches the association key.

You can rename your `bookmark` property to `entry_bookmark`.

Or you can change the association key with the `forKey(_:)` method:

```swift
static let bookmark = belongsTo(...).forKey("bookmark")

```

The `forKey(_:)` method is useful, notably, when table names do not match the names of their Swift counterpart, such as `entry_bookmark` vs. `bookmark`

See [The Structure of a Joined Request](https://github.com/groue/GRDB.swift/blob/master/Documentation/AssociationsBasics.md#the-structure-of-a-joined-request) for more information about association keys and the `forKey(_:)` method.
