11defmodule AshPostgres do
22@ using_opts_schema Ashton . schema (
33opts: [
4- repo: :atom
4+ repo: :atom ,
5+ table: :string
56] ,
67required: [ :repo ] ,
78describe: [
89repo:
9- "The repo that will be used to fetch your data. See the `Ecto.Repo` documentation for more"
10+ "The repo that will be used to fetch your data. See the `Ecto.Repo` documentation for more" ,
11+ table: "The name of the database table backing the resource"
1012] ,
1113constraints: [
1214repo:
@@ -31,10 +33,15 @@ defmodule AshPostgres do
3133
3234@ data_layer AshPostgres
3335@ repo opts [ :repo ]
36+ @ table opts [ :table ]
3437
3538def repo ( ) do
3639@ repo
3740end
41+
42+ def postgres_table ( ) do
43+ @ table || @ name
44+ end
3845end
3946end
4047
@@ -217,13 +224,16 @@ defmodule AshPostgres do
217224end
218225
219226defp do_join_relationship ( query , [ % { type: :many_to_many } = relationship ] , :inner ) do
227+ relationship_through = maybe_get_resource_query ( relationship . through )
228+ relationship_destination = maybe_get_resource_query ( relationship . destination )
229+
220230new_query =
221231from ( row in query ,
222- join: through in ^ relationship . through ,
232+ join: through in ^ relationship_through ,
223233on:
224234field ( row , ^ relationship . source_field ) ==
225235field ( through , ^ relationship . source_field_on_join_table ) ,
226- join: destination in ^ relationship . destination ,
236+ join: destination in ^ relationship_destination ,
227237on:
228238field ( destination , ^ relationship . destination_field ) ==
229239field ( through , ^ relationship . destination_field_on_join_table )
@@ -238,23 +248,28 @@ defmodule AshPostgres do
238248end
239249
240250defp do_join_relationship ( query , [ relationship ] , :inner ) do
251+ relationship_destination = maybe_get_resource_query ( relationship . destination )
252+
241253new_query =
242254from ( row in query ,
243- join: destination in ^ relationship . destination ,
255+ join: destination in ^ relationship_destination ,
244256on: field ( row , ^ relationship . source_field ) == field ( row , ^ relationship . destination_field )
245257)
246258
247259add_binding ( new_query , [ relationship . name ] , :inner )
248260end
249261
250262defp do_join_relationship ( query , [ % { type: :many_to_many } = relationship ] , :left ) do
263+ relationship_through = maybe_get_resource_query ( relationship . through )
264+ relationship_destination = maybe_get_resource_query ( relationship . destination )
265+
251266new_query =
252267from ( row in query ,
253- left_join: through in ^ relationship . through ,
268+ left_join: through in ^ relationship_through ,
254269on:
255270field ( row , ^ relationship . source_field ) ==
256271field ( through , ^ relationship . source_field_on_join_table ) ,
257- left_join: destination in ^ relationship . destination ,
272+ left_join: destination in ^ relationship_destination ,
258273on:
259274field ( destination , ^ relationship . destination_field ) ==
260275field ( through , ^ relationship . destination_field_on_join_table )
@@ -269,9 +284,11 @@ defmodule AshPostgres do
269284end
270285
271286defp do_join_relationship ( query , [ relationship ] , :left ) do
287+ relationship_destination = maybe_get_resource_query ( relationship . destination )
288+
272289new_query =
273290from ( row in query ,
274- left_join: destination in ^ relationship . destination ,
291+ left_join: destination in ^ relationship_destination ,
275292on: field ( row , ^ relationship . source_field ) == field ( row , ^ relationship . destination_field )
276293)
277294
@@ -433,4 +450,12 @@ defmodule AshPostgres do
433450def transaction ( resource , func ) do
434451repo ( resource ) . transaction ( func )
435452end
453+
454+ defp maybe_get_resource_query ( resource ) do
455+ if Ash . resource_module? ( resource ) do
456+ { resource . postgres_table ( ) , resource }
457+ else
458+ resource
459+ end
460+ end
436461end