ash-project / ash_postgres

The PostgreSQL data layer for Ash Framework
https://hexdocs.pm/ash_postgres
MIT License
134 stars 73 forks source link

update tests to show many_to_many filter problem #309

Closed barnabasJ closed 3 months ago

barnabasJ commented 3 months ago

I was able to recreate the problem mentioned in https://github.com/ash-project/ash/issues/1192

It only manifests if the identities/primary_keys are setup differently

barnabasJ commented 3 months ago

Yes, the state == :active filter is missing from the query

On Tue, 28 May 2024, 16:54 Zach Daniel, @.***> wrote:

@.**** commented on this pull request.

In test/support/resources/post.ex https://github.com/ash-project/ash_postgres/pull/309#discussion_r1617406146 :

@@ -324,6 +324,19 @@ defmodule AshPostgres.Test.Post do read_action: :active )

  • has_many :active_followers_assoc, AshPostgres.Test.PostFollower do
  • public?(true)
  • filter(expr(state == :active))
  • end
  • many_to_many(:active_followers, AshPostgres.Test.User,
  • public?: true,
  • through: AshPostgres.Test.PostFollower,
  • join_relationship: :first_follower_assoc,

Is this the issue? Its not using :active_followers_assoc?

— Reply to this email directly, view it on GitHub https://github.com/ash-project/ash_postgres/pull/309#pullrequestreview-2083076504, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACZBCTKJD6K5UPPXWQJ7BLTZESLBLAVCNFSM6AAAAABIMSYCY2VHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDAOBTGA3TMNJQGQ . You are receiving this because you were assigned.Message ID: @.***>

zachdaniel commented 3 months ago

but its not using the has_many relationship that has that filter. Its creating its own has_many relationship without a filter.

barnabasJ commented 3 months ago

but its not using the has_many relationship that has that filter. Its creating its own has_many relationship without a filter.

I'm not sure I get what your are trying to tell me. I think it should use the active_followers_assoc according to the config, and it did until I changed the primary keys inside PostFollowers.

zachdaniel commented 3 months ago
    has_many :active_followers_assoc, AshPostgres.Test.PostFollower do
      public?(true)
      filter(expr(state == :active))
    end

    many_to_many(:active_followers, AshPostgres.Test.User,
      public?: true,
      through: AshPostgres.Test.PostFollower,
      join_relationship: :first_follower_assoc, # <- this creates a new `has_many` relationship called `:first_follower_assoc`
      source_attribute_on_join_resource: :post_id,
      destination_attribute_on_join_resource: :follower_id
    )
barnabasJ commented 3 months ago

facepalm

Sorry, I also had a typo in my project code.

barnabasJ commented 3 months ago

Fixing the typo in my project didn't fix it. But now I think I have it reproduced correctly

barnabasJ commented 3 months ago

@zachdaniel can you please take another look

11:05:41.239 [debug] QUERY OK source="posts" db=0.2ms queue=0.5ms
SELECT p0."id", p0."title_column", p0."datetime", p0."score", p0."public", p0."category", p0."type", p0."price", p0."decimal", p0."status", p0."status_enum", p0."status_enum", p0."point", p0."composite_point", p0."stuff", p0."list_of_stuff", p0."uniq_one", p0."uniq_two", p0."uniq_custom_one", p0."uniq_custom_two", p0."uniq_on_upper", p0."uniq_if_contains_foo", p0."list_containing_nils", p0."created_at", p0."updated_at", p0."organization_id", p0."author_id" FROM "posts" AS p0 WHERE (p0."type"::varchar = $1::varchar) [:sponsored]

11:05:41.244 [debug] QUERY OK source="stateful_post_followers" db=0.1ms queue=0.2ms
SELECT s0."post_id", s0."follower_id", s0."id" FROM "stateful_post_followers" AS s0 WHERE (s0."post_id"::uuid IN ($1::uuid)) ["e00842db-fd87-488a-8bc8-7782ea005ecf"]

11:05:41.245 [debug] QUERY OK source="users" db=0.2ms queue=0.1ms
SELECT u0."id", u0."is_active", u0."name", u0."organization_id" FROM "users" AS u0 WHERE (u0."id"::uuid IN ($1::uuid,$2::uuid,$3::uuid)) ["0fc19e5e-df74-4fbc-bec4-9cedc4fd1cff", "79ddccab-ceb5-4f1e-bbef-595ef105c69d", "a96f0d9c-8101-4913-a2e3-5e3bd5dfb32e"]

Now the definition should be correct, but the resulting SQL does not take into account the filter on the join relationship

zachdaniel commented 3 months ago

Fixed in latest ash