问题
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