On Friday, August 7, 2020 at 1:08:04 AM UTC-7, Petr Kaleta wrote:
>
> Hello everyone!
>
> I have project with two databases. Let's call them DB and SHARED_DB. For 
> example I have two models:
>
> class Foo < Sequel::Model(DB[:foos])
>   many_to_one :bar
> end
>
>
> class Bar < Sequel::Model(SHARED_DB[:bars])
>   one_to_many :foos
> end
>
>
> I need to find some best way, how to validate during create/update that 
> referenced bar_id really belongs to any bar... Traditional foreign_key in 
> Postgresql is the best way but in my case we have two separate databases 
> here. Have you ever solved the same problem?
>  
> Thank you
>

You can do something like:

class Foo
  def validate
    super
    errors.add(:bar_id, 'no matching bar') if bar_id && !bar
  end
end

It's definitely not as good as a real foreign key, but I guess it is better 
than nothing.

Thanks,
Jeremy

-- 
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/642984be-6cbe-4365-b426-19fdfb227632o%40googlegroups.com.

Reply via email to