1
votes

In my umbrella app, with currently two apps, each based on its own DB...

I have a schema in app2 referencing a uuid from an object of app1.

DB1 :
  Table : parent
    parent_id: uuid
    name: string

DB2 :
Table : child
    child_id: uuid
    parent_id: uuid
    name: string

Then the schema :

  schema "child" do
    field :name, :string
    belongs_to(:parent, Parent, type: :binary_id)

    timestamps()
  end

How to have the changeset validate that parent_id is valid and exists in its table ? I tried some things, like assoc_constraint(:parent) but tbh I knew it wouldn't work... Maybe I just should rethink the relationship and how it works.

Thanks ahead.

1

1 Answers

2
votes

The short answer is: No, it is not possible to have relations between different databases out of the box. So you can not have that belongs_to(:parent, Parent, type: :binary_id) in Child if parent is a table in a different database.

The long answer is that it could be possible, depending on your RDBMS, to define relations between databases. For example Postgres has some extensions like dblink and postgres_fdw that are briefly described here.