5

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?

Community
  • 1
  • 1
王志軍
  • 1,021
  • 1
  • 11
  • 21
  • 3
    Please include the specific error you are getting. – Patrick Oscity May 27 '15 at 05:50
  • 1
    Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). BTW, it's "Thanks in advance", not "Thanks in advanced". – John Saunders May 27 '15 at 05:50
  • 1
    Please include the error you are getting and the Ecto version. Also notice that you have a typo in your query. It should be `, on: cucu` and not `, on cucu`. – José Valim May 27 '15 at 07:50
  • 1
    Please do not post screenshots, instead paste the code in your question and format it properly using the `{}` button in the editor. You should only post relevant parts of the code, not unnecessary comments. If you want to get good answers, you should make it as easy as possible for the readers. – Patrick Oscity May 27 '15 at 11:34

1 Answers1

7

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

José Valim
  • 50,409
  • 12
  • 130
  • 115
  • José, It worked!! Really appreciate your work! In order to try the master ecto, I need to change the `mix.exs` file . `{:ecto, github: "elixir-lang/ecto"},` – 王志軍 May 29 '15 at 01:44