在Elixir Ecto中加入两个属于两个数据库的表

王志軍*_*王志軍 5 elixir ecto

在Elixir中,使用Ecto,可以连接属于不同两个数据库的两个不同的表(在同一主机中).

有两个数据库称为cloudcloud_usage在此查询

当我执行查询时,我应该使用哪个Repo?

Billing.CloudUsage.Repo.all(query)

要么

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)
Run Code Online (Sandbox Code Playgroud)

当我调用该函数时,我收到了错误

** (Mariaex.Error) (1146): Table 'cloud_usage.cloud_usage.cloud_usage' doesn't exist
Run Code Online (Sandbox Code Playgroud)

我知道为什么会这样.但如果我使用Billing.Cloud.Repo.all(query),我想我几乎无法检索cloud_usage.cloud_usage表中的数据.反之亦然

参考:

MySQL - 在两个不同的数据库中的表之间连接?

Jos*_*lim 7

你的代码很完美.这是Ecto中的一个错误.如果你想尝试一下,我已经把它修好了.:)