# Issue: Can't create custom relation

**URL:** <https://discourse.rom-rb.org/t/issue-cant-create-custom-relation/166>\
**Category:** Issues\
**Created:** [July 19, 2017, 9:56am UTC](https://discourse.rom-rb.org/t/issue-cant-create-custom-relation/166 "2017-07-19T09:56:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![opan](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/opan/32/105_2.png) [@opan](https://discourse.rom-rb.org/u/opan)\
**Post date:** [July 19, 2017, 9:56am UTC](https://discourse.rom-rb.org/t/issue-cant-create-custom-relation/166/1 "2017-07-19T09:56:11Z")

</div>

Hi, I have a problem while trying to create a custom relation in the repository. Here is my code.

#### Migration

```ruby

ROM::SQL.migration do
  change do
    create_table(:banks) do
      primary_key :id, type: :Bignum
      foreign_key :party_id, :parties, type: :Bignum, null: false, index: true
      foreign_key :bank_type_id, :bank_types, type: :Bignum, index: true
      foreign_key :country_id, :regions, type: :Bignum, index: true
      foreign_key :district_id, :regions, type: :Bignum, index: true

      String :code, size: 255, null: false, index: true
      Date :from_date, null: false
      Date :thru_date
      String :swift_code, size: 255, index: true
      String :other_country, size: 255, index: true
      String :other_district, size: 255, index: true
      String :postal_address, size: 255, index: true
      DateTime :created_at
      Bignum :created_by_id
      DateTime :updated_at
      Bignum :updated_by_id
    end
  end

  change do
    create_table(:regions) do
      primary_key :id, type: :Bignum

      String :region_type, size: 255, null: false, index: true
      Bignum :parent_id, index: true
      String :code, size: 255, null: false, index: true
      String :name, size: 255, null: false, index: true
      String :postal_code, size: 255
      DateTime :created_at
      Bignum :created_by_id
      DateTime :updated_at
      Bignum :updated_by_id
    end
  end

end

```

#### My code

```ruby

db = ROM.container(:sql, conn_string) do |conf|
  class Countries < ROM::Relation[:sql]
    schema(:regions, as: :countries, infer: true)
  end
  conf.gateways[:default].run_migrations
  conf.register_relation Countries
end

```

I execute this code through rspec, but I always get this errors:

```auto
ROM::Registry::ElementNotFoundError:
  :regions doesn't exist in ROM::RelationRegistry registry
# ./system/boot/persistence.rb:41:in `new'
# ./system/boot/persistence.rb:41:in `block (3 levels) in <module:Procura>'
# ./system/boot/persistence.rb:38:in `each'
# ./system/boot/persistence.rb:38:in `block (2 levels) in <module:Procura>'
# ./system/env/test.rb:2:in `<module:Procura>'
# ./system/env/test.rb:1:in `<top (required)>'
# ./system/application.rb:6:in `require_relative'
# ./system/application.rb:6:in `<class:Application>'
# ./system/application.rb:4:in `<module:Procura>'
# ./system/application.rb:3:in `<top (required)>'
# ./system/boot.rb:12:in `require_relative'
# ./system/boot.rb:12:in `<top (required)>'
# ./spec/spec_helper.rb:9:in `require_relative'
# ./spec/spec_helper.rb:9:in `<top (required)>'
# ./spec/lib/procura_engine/repository/bank_repo_spec.rb:3:in `<top (required)>'

```

The errors say that table `regions` is didn’t exists, in fact, that table is already created. Is there something I missed? I’m following this guide, [here](https://github.com/rom-rb/rom/issues/414) and [here](https://github.com/rom-rb/rom-sql/issues/91).

Thank you

---

<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:** [July 20, 2017, 10:24pm UTC](https://discourse.rom-rb.org/t/issue-cant-create-custom-relation/166/2 "2017-07-20T22:24:26Z")

</div>

Instead of using migrations, try to simply do `conf.gateways[:default].create_table { ... }`. This should work.

---

<div class="post-metadata">

**Author:** ![opan](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/opan/32/105_2.png) [@opan](https://discourse.rom-rb.org/u/opan)\
**Post date:** [July 24, 2017, 2:12am UTC](https://discourse.rom-rb.org/t/issue-cant-create-custom-relation/166/3 "2017-07-24T02:12:49Z")

</div>

@solnic Thank you for the response and sorry for my late response. But we can do that because we use Sequel migration feature. Is there a way to make it works by using migration files?

---

<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:** [July 24, 2017, 1:03pm UTC](https://discourse.rom-rb.org/t/issue-cant-create-custom-relation/166/4 "2017-07-24T13:03:50Z")

</div>

Can you describe your setup? There should be no need to use migrations within rom configuration/container block.

---

<div class="post-metadata">

**Author:** ![opan](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/opan/32/105_2.png) [@opan](https://discourse.rom-rb.org/u/opan)\
**Post date:** [July 25, 2017, 3:08am UTC](https://discourse.rom-rb.org/t/issue-cant-create-custom-relation/166/5 "2017-07-25T03:08:08Z")

</div>

@solnic Well, I use many of DRY module. But finally, I can make it works even with migrations feature from Sequel.

Here is the updated code:

```ruby
      class Regions < Pelita::Relation::Base[:sql]
        schema(:regions, infer: true)
      end

      class Countries < Pelita::Relation::Base[:sql]
        schema(:regions, as: :countries, infer: true)
      end

      class Districts < Pelita::Relation::Base[:sql]
        schema(:regions, as: :districts, infer: true)
      end

```

The `:regions doesn't exist in ROM::RelationRegistry registry` errors occur because I don’t register the `regions`. After I do that, this issue was solved.
