# Association not being loaded if foreign key is aliased

**URL:** https://discourse.rom-rb.org/t/association-not-being-loaded-if-foreign-key-is-aliased/296
**Category:** Issues
**Created:** [December 28, 2018, 6:58pm UTC](https://discourse.rom-rb.org/t/association-not-being-loaded-if-foreign-key-is-aliased/296 "2018-12-28T18:58:49Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![waiting-for-dev](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/waiting-for-dev/32/164_2.png) [@waiting-for-dev](https://discourse.rom-rb.org/u/waiting-for-dev)
#### Post date: [December 28, 2018, 6:58pm UTC](https://discourse.rom-rb.org/t/association-not-being-loaded-if-foreign-key-is-aliased/296/1 "2018-12-28T18:58:49Z")

</div>

Say we have two relations:

```ruby
class Registrations < ROM::Relation[:sql]
  schema(:registrations, infer: true)

  associations do
    belongs_to :client
  end
end

```

```ruby
class Clients < ROM::Relation[:sql]
  schema(:clients, infer: true)
end

```

Doing the following works as expected:

```ruby
Container.relations[:registrations].combine(:client).first

```

However, if we add the following alias in the `Registrations` relation to the foreign key attribute:

```ruby
attribute :cliente_id, Types::Int.optional.meta(alias: :client_id)

```

Now, `combine` will always return `nil` as the `client` key, even when `client_id` attribute is populated.

Trying to specify the aliased name as foreign key:

```ruby
    belongs_to :client, foreign_key: :client_id

```

fails with:

```auto
KeyError: :client_id attribute doesn't exist in registrations schema

```

While specifying the actual table name in as foreign\_key:

```ruby
    belongs_to :client, foreign_key: :cliente_id

```

doesn’t solve the issue.

Is there something else expected to be performed on my side or is it a bug?

---

<div class="post-metadata">

### Author: ![solnic](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/solnic/32/279_2.png) [@solnic](https://discourse.rom-rb.org/u/solnic)
#### Post date: [December 29, 2018, 11:31am UTC](https://discourse.rom-rb.org/t/association-not-being-loaded-if-foreign-key-is-aliased/296/2 "2018-12-29T11:31:19Z")

</div>

This is a bug. Please report in `rom-rb/rom`.
