On Monday, August 10, 2020 at 7:58:16 AM UTC-7, Petr Kaleta wrote:
>
> Hello everyone,
> I have two separate databases DB and SHARED_DB.
>
> class Foo < Sequel::Model(DB[:foos])
> many_to_many :bars, class: 'Bar', join_table: DB[:foo_bars]
> end
>
>
> class Bar < Sequel::Model(SHARED_DB[:bars])
> end
>
>
> Problem is, that I am getting SQL error that table foo_bars does not exist
> in SHARED_DB. Another associations like one_to_many and many_to_one works
> with no issues... I understand that it is not possible to make a join
> across multiple databases, but I thought that Sequel can handle it and
> loads Bars by its primary keys...
>
No, Sequel always used a join table for many_to_many associations. The
approach below shows a working example.
Thanks,
Jeremy
DB = Sequel.sqlite
SHARED_DB = Sequel.sqlite
DB.create_table(:foos){primary_key :id}
DB.create_table(:foo_bars){Integer :foo_id; Integer :bar_id}
SHARED_DB.create_table(:bars){primary_key :id}
class Foo < Sequel::Model(DB[:foos])
unrestrict_primary_key
one_to_many :foo_bars
one_to_many :bars, :dataset=>proc{|r|
r.associated_dataset.where(:id=>foo_bars_dataset.select_map(:bar_id))}
end
class FooBar < Sequel::Model(DB[:foo_bars])
end
class Bar < Sequel::Model(SHARED_DB[:bars])
unrestrict_primary_key
end
foo = Foo.create(:id=>1)
Bar.create(:id=>2)
Bar.create(:id=>3)
Bar.create(:id=>4)
FooBar.create(:foo_id=>1, :bar_id=>2)
FooBar.create(:foo_id=>1, :bar_id=>3)
FooBar.create(:foo_id=>2, :bar_id=>4)
p foo.bars
[#<Bar @values={:id=>2}>, #<Bar @values={:id=>3}>]
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sequel-talk/37430b0b-2f4c-418d-bfc4-944e2ae4604bo%40googlegroups.com.