Search code examples
elixirecto

Join two tables belong to two database in Elixir Ecto


In Elixir, with Ecto, is it possible to join two different tables (in the same host) belonging to different two databases.

There are two databases called cloud and cloud_usage in this query

When I execute the query, which Repo should I use?

Billing.CloudUsage.Repo.all(query)

or

Billing.Cloud.Repo.all(query)

    query = from cucu in "cloud_usage.cloud_usage",
        inner_join: cv in "cloud.volumes", on: cucu.usage_id == cv.id,
          where: cucu.account_id == ^account_id,
          where: cucu.usage_id == 6,
          where: like(cucu.description, ^vol_description),
          where: cucu.start_date >= ^start_datetime,
          where: cucu.start_date <= ^end_datetime,
       group_by: cucu.usage_id,
       group_by: cucu.zone_id,
         select: {cucu.usage_id, cucu.zone_id, cucu.size, sum(cucu.raw_usage)}
   result  = Billing.CloudUsage.Repo.all(query)

When I call the function I got the error

** (Mariaex.Error) (1146): Table 'cloud_usage.cloud_usage.cloud_usage' doesn't exist

I know why this happened. But If I use Billing.Cloud.Repo.all(query) , I think I can hardly retrieve the data in cloud_usage.cloud_usage table. Vice versa

Reference:

MySQL -- join between tables in 2 different databases?


Solution

  • Your code is perfect. This is a bug in Ecto. I have fixed it in master if you want to give it a try. :)