@@ -263,9 +263,16 @@ defmodule AshPostgres.DataLayer do
263263
264264@ impl true
265265def run_query ( query , resource ) do
266- { :ok , repo ( resource ) . all ( query , repo_opts ( query ) ) }
266+ if AshPostgres . polymorphic? ( resource ) && no_table? ( query ) do
267+ raise_table_error! ( resource , :read )
268+ else
269+ { :ok , repo ( resource ) . all ( query , repo_opts ( query ) ) }
270+ end
267271end
268272
273+ defp no_table? ( % { from: % { source: { "" , _ } } } ) , do: true
274+ defp no_table? ( _ ) , do: false
275+
269276defp repo_opts ( % Ash.Changeset { tenant: tenant , resource: resource } ) do
270277repo_opts ( % { tenant: tenant , resource: resource } )
271278end
@@ -481,7 +488,7 @@ defmodule AshPostgres.DataLayer do
481488defp ecto_changeset ( record , changeset , type ) do
482489ecto_changeset =
483490record
484- |> set_table ( changeset )
491+ |> set_table ( changeset , type )
485492|> Ecto.Changeset . change ( changeset . attributes )
486493
487494case type do
@@ -506,17 +513,14 @@ defmodule AshPostgres.DataLayer do
506513end
507514end
508515
509- defp set_table ( record , changeset ) do
516+ defp set_table ( record , changeset , operation ) do
510517if AshPostgres . polymorphic? ( record . __struct__ ) do
511518table = changeset . context [ :data_layer ] [ :table ] || AshPostgres . table ( record . __struct )
512519
513520if table do
514521Ecto . put_meta ( record , source: table )
515522else
516- raise """
517- Attempted to change a polymorphic resource without setting the `table` context,
518- and without a default table configured on the resource.
519- """
523+ raise_table_error! ( changeset . resource , operation )
520524end
521525else
522526record
@@ -1824,4 +1828,19 @@ defmodule AshPostgres.DataLayer do
18241828defp table ( resource , changeset ) do
18251829changeset . context [ :data_layer ] [ :table ] || AshPostgres . table ( resource )
18261830end
1831+
1832+ defp raise_table_error! ( resource , operation ) do
1833+ if AshPostgres . polymorphic? ( resource ) do
1834+ raise """
1835+ Could not determine table for#{ operation } on#{ inspect ( resource ) } .
1836+
1837+ Polymorphic resources require that the `data_layer[:table]` context is provided.
1838+ See the guide on polymorphic resources for more information.
1839+ """
1840+ else
1841+ raise """
1842+ Could not determine table for#{ operation } on#{ inspect ( resource ) } .
1843+ """
1844+ end
1845+ end
18271846end