Issue: Can't create custom relation


#1

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

Migration


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


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:

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 and here.

Thank you


#2

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


#3

@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?


#4

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


#5

@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:

      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.