Using `cast_assoc` to associate records on different schemas

蹲街弑〆低调 提交于 2019-12-13 03:45:58

问题


I'm trying to use cast_assoc to associate records that exist on different schemas.

In the code below, Organization exists in a tenant schema (e.g. "tenant_2837.organizations"), whereas Workspace exists on the public schema (e.g. "public.workspaces").

When the code runs, Ecto tries to create the Workspace under the tenant schema.

%Organization{}
|> Organization.create_organization_changeset(attrs)
|> cast_assoc(:workspace, with: &Workspace.changeset/2)
|> Repo.insert(prefix: TenantActions.build_prefix(tenant))

Is there a way to force it to create it under the schema specified as the @schema_prefix module attribute? i.e.

defmodule MyApp.Workspaces.Workspace do
  use Ecto.Schema

  @schema_prefix "public"

  schema "workspaces" do
    field :subdomain, :string
    field :name, :string
    belongs_to :organization, Organizations.Organization

    timestamps(type: :utc_datetime_usec)
  end
end

来源:https://stackoverflow.com/questions/56349799/using-cast-assoc-to-associate-records-on-different-schemas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!