# Combines with an association with composite keys

**URL:** https://discourse.rom-rb.org/t/combines-with-an-association-with-composite-keys/321
**Category:** Issues
**Created:** [June 14, 2019, 8:22pm UTC](https://discourse.rom-rb.org/t/combines-with-an-association-with-composite-keys/321 "2019-06-14T20:22:23Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ethnt](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/ethnt/32/167_2.png) [@ethnt](https://discourse.rom-rb.org/u/ethnt)
#### Post date: [June 14, 2019, 8:22pm UTC](https://discourse.rom-rb.org/t/combines-with-an-association-with-composite-keys/321/1 "2019-06-14T20:22:23Z")

</div>

Hi,

We have the following schema:

```
class Numbers < ROM::Relation[:sql]
  schema(:numbers, infer: true) do
    attribute :company_id, Types::Int
    attribute :region_id, Types::Int

    associations do
      has_one :identities, view: :for_numbers, override: true
    end
  end
end

class Identities < ROM::Relation[:sql]
  schema(:identities, infer: true) do
    attribute :company_id, Types::Int
    attribute :region_id, Types::Int
  end

  def for_numbers(_assoc, _identities)
    join(:numbers, { region_id: :region_id, company_id: :company_id })
  end
end

```

We want to be able to `combine` the `identities` into `numbers` by associating them. They’re only “loosely” related, not strictly related — a number will be associated with an identity if they both have the same `region_id` and `company_id`.

For whatever reason though, when I use combine, I always get an empty `identity` value in our struct:

```
numbers.combine(:identity).by_pk(1).one # => { id: 1, company_id: 42, region_id: 28, identity: nil }

```

I’m not sure why this is happening — if I run that join normally on `identities`, I’ll get the correct entities.

---

<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: [June 18, 2019, 9:31pm UTC](https://discourse.rom-rb.org/t/combines-with-an-association-with-composite-keys/321/2 "2019-06-18T21:31:29Z")

</div>

@ethnt is this a legacy schema or is it something that you put together and can still change it? I’m asking because using composite keys is _not recommended_ due to all the potential issues you may face.

Anyhow, your attribute definitions are missing FK info, you need to do `attribute :company_id, Types.ForeignKey(:companies)` otherwise rom will not know that it’s a foreign key. Secondly, you need to provide combine keys in the association definition, so `has_one :identities, ..., combine_keys: %i[region_id company_id]`.

Please try this and let me know if this worked.
