How to write relationships with no_attributes?

This is Product.relationships code.

This is load test code for above relationships.

I think three stores, stores_alt1, stores_alt2 relationships should be equivalent.
But, only the stores relationship works,
and other two relationships raise Ecto.Query.CompileError.

I’m confused how to write relationship with no_attributes? true.
Is there any proper way to write this kind of relationships correctly? @zachdaniel

What are the details of the error? Just knowing Ecto.Query.CompileError is not really useful without more information.

With that said, could you also describe specifically what the stores relationship is meant to provide?

I can’t really give advice as I don’t know what you’re specifically looking to do.

Here are the stacktraces.

stacktrace for stores_alt1

  1) test relationships stores (SonetLib.SevenEleven.ProductTest)
     test/sonet_lib/seven_eleven/product_test.exs:24
     ** (Ecto.Query.CompileError) only one distinct expression is allowed in query
     code: Ashex.load!(product, [:stores_alt1])
     stacktrace:
       (ash_postgres 2.4.11) lib/data_layer.ex:1102: AshPostgres.DataLayer.lateral_join_query/3
       (ash_postgres 2.4.11) lib/data_layer.ex:961: AshPostgres.DataLayer.run_query_with_lateral_join/4
       (ash 3.4.35) lib/ash/actions/read/read.ex:2650: Ash.Actions.Read.run_query/4
       (ash 3.4.35) lib/ash/actions/read/read.ex:542: anonymous fn/8 in Ash.Actions.Read.do_read/5
       (ash 3.4.35) lib/ash/actions/read/read.ex:886: Ash.Actions.Read.maybe_in_transaction/3
       (ash 3.4.35) lib/ash/actions/read/read.ex:269: Ash.Actions.Read.do_run/3
       (ash 3.4.35) lib/ash/actions/read/read.ex:81: anonymous fn/3 in Ash.Actions.Read.run/3
       (ash 3.4.35) lib/ash/actions/read/read.ex:80: Ash.Actions.Read.run/3
       (ash 3.4.35) lib/ash/actions/read/relationships.ex:413: anonymous fn/2 in Ash.Actions.Read.Relationships.do_fetch_related_records/4
       (ash 3.4.35) lib/ash/actions/read/relationships.ex:65: Ash.Actions.Read.Relationships.fetch_related_records/4
       (ash 3.4.35) lib/ash/actions/read/relationships.ex:24: Ash.Actions.Read.Relationships.load/4
       (ash 3.4.35) lib/ash/actions/read/read.ex:298: Ash.Actions.Read.do_run/3
       (ash 3.4.35) lib/ash/actions/read/read.ex:81: anonymous fn/3 in Ash.Actions.Read.run/3
       (ash 3.4.35) lib/ash.ex:1859: Ash.load/3
       (ash 3.4.35) lib/ash.ex:1813: Ash.load/3
       (ash 3.4.35) lib/ash.ex:1754: Ash.load!/3
       test/sonet_lib/seven_eleven/product_test.exs:28: (test)

stacktrace for stores_alt2

  1) test relationships stores (SonetLib.SevenEleven.ProductTest)
     test/sonet_lib/seven_eleven/product_test.exs:24
     ** (Ecto.Query.CompileError) only one distinct expression is allowed in query
     code: Ashex.load!(product, [:stores_alt2])
     stacktrace:
       (ash_postgres 2.4.11) lib/data_layer.ex:1102: AshPostgres.DataLayer.lateral_join_query/3
       (ash_postgres 2.4.11) lib/data_layer.ex:961: AshPostgres.DataLayer.run_query_with_lateral_join/4
       (ash 3.4.35) lib/ash/actions/read/read.ex:2650: Ash.Actions.Read.run_query/4
       (ash 3.4.35) lib/ash/actions/read/read.ex:542: anonymous fn/8 in Ash.Actions.Read.do_read/5
       (ash 3.4.35) lib/ash/actions/read/read.ex:886: Ash.Actions.Read.maybe_in_transaction/3
       (ash 3.4.35) lib/ash/actions/read/read.ex:269: Ash.Actions.Read.do_run/3
       (ash 3.4.35) lib/ash/actions/read/read.ex:81: anonymous fn/3 in Ash.Actions.Read.run/3
       (ash 3.4.35) lib/ash/actions/read/read.ex:80: Ash.Actions.Read.run/3
       (ash 3.4.35) lib/ash/actions/read/relationships.ex:413: anonymous fn/2 in Ash.Actions.Read.Relationships.do_fetch_related_records/4
       (ash 3.4.35) lib/ash/actions/read/relationships.ex:65: Ash.Actions.Read.Relationships.fetch_related_records/4
       (ash 3.4.35) lib/ash/actions/read/relationships.ex:24: Ash.Actions.Read.Relationships.load/4
       (ash 3.4.35) lib/ash/actions/read/read.ex:298: Ash.Actions.Read.do_run/3
       (ash 3.4.35) lib/ash/actions/read/read.ex:81: anonymous fn/3 in Ash.Actions.Read.run/3
       (ash 3.4.35) lib/ash.ex:1859: Ash.load/3
       (ash 3.4.35) lib/ash.ex:1813: Ash.load/3
       (ash 3.4.35) lib/ash.ex:1754: Ash.load!/3
       test/sonet_lib/seven_eleven/product_test.exs:32: (test)

I will explain more about my question.

There are three resources: Store, Inventory, Product.
Store and Product is simple resource, and Inventory is join table resource.

  • Store
    has_many :inventories
  • Product
    has_many :inventories
  • Inventory
    belongs_to :store
    belongs_to :product

I know there is many_to_many feature,
but I wanted to show you there are some hard things when using no_attributes? true.

A private project in our company uses no_attributes? true a lot
(There are some scenarios our company have, only no_attributes? true can be a solution.),
and I meet this kind of error a lot.
How can I avoid these errors and write correct relationship?
Or is this an bug?

** (Ecto.Query.CompileError) only one distinct expression is allowed in query
is a bug.

Please open a bug report.

If you can, please provide a reproduction and as much detail as you can. Ideally a test I can run that reproduces the specific error :smiley:

I opened a bug report about this.

Thank you, I’ll take a look in the next day or two.

This is fixed in main of ash_postgres. I will release new versions of ash and ash_postgres this week.

2 Likes

Thanks.

The bug I reported is fixed, but I found another one related to original one.
I opened a new issue in ash_postgres.