如何在Repo。*()级别的`many_to_many`assoc`中的`join_through`表中添加前缀?

时间:2019-05-03 01:38:46

标签: ecto

我正在尝试查询具有前缀的many_to_many关系,如下所示:

Student
|> join(:left, [s], t in assoc(s, :teachers))
|> Repo.all(prefix: "my_prefix")

这将导致PostgreSQL查询:

SELECT s0."id", s0."name", s0."inserted_at", s0."updated_at"
FROM "my_prefix"."students" AS s0
LEFT OUTER JOIN "teachers_students" AS t2 ON t2."student_id" = s0."id"
LEFT OUTER JOIN "my_prefix"."teachers" AS t1 ON t2."teacher_id" = t1."id"

我希望将前缀添加到join_throughteacher_students中,但是不会添加该前缀。这是Ecto中的错误吗?还是有解决方法?

1 个答案:

答案 0 :(得分:0)

看起来我能够通过将join_through定义中的many_to_many值从字符串更改为模块名称来解决此问题:

schema "students" do
  # many_to_many :teachers, Teacher, join_through: "teachers_students", on_replace: :delete
  many_to_many :teachers, Teacher, join_through: TeachersStudents, on_replace: :delete
  ...
end
defmodule MyApp.TeachersStudents do
  use Ecto.Schema
  alias MyApp.Teachers.Teacher
  alias MyApp.Students.Student

  schema "teachers_students" do
    belongs_to :teacher, Teacher
    belongs_to :student, Student
  end
end

Ecto忽略字符串值的前缀可能是一个错误。