Skip to content

Commit a7d73a3

Browse files
authored
demonstrate bug with joining across aggregates (#671)
1 parent c85f18b commit a7d73a3

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"attributes": [
3+
{
4+
"allow_nil?": false,
5+
"default": "fragment(\"gen_random_uuid()\")",
6+
"generated?": false,
7+
"precision": null,
8+
"primary_key?": true,
9+
"references": null,
10+
"scale": null,
11+
"size": null,
12+
"source": "id",
13+
"type": "uuid"
14+
},
15+
{
16+
"allow_nil?": true,
17+
"default": "nil",
18+
"generated?": false,
19+
"precision": null,
20+
"primary_key?": false,
21+
"references": null,
22+
"scale": null,
23+
"size": null,
24+
"source": "title",
25+
"type": "text"
26+
},
27+
{
28+
"allow_nil?": false,
29+
"default": "0",
30+
"generated?": false,
31+
"precision": null,
32+
"primary_key?": false,
33+
"references": null,
34+
"scale": null,
35+
"size": null,
36+
"source": "importance",
37+
"type": "bigint"
38+
}
39+
],
40+
"base_filter": null,
41+
"check_constraints": [],
42+
"create_table_options": null,
43+
"custom_indexes": [],
44+
"custom_statements": [],
45+
"has_create_action": true,
46+
"hash": "8357381E60B641F3A2F66AF2B3830CCFADFB6EFC281D65A0AA4BCD714A611B0E",
47+
"identities": [],
48+
"multitenancy": {
49+
"attribute": null,
50+
"global": null,
51+
"strategy": null
52+
},
53+
"repo": "Elixir.AshPostgres.TestRepo",
54+
"schema": null,
55+
"table": "tags"
56+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
defmodule AshPostgres.TestRepo.Migrations.AddTitleToTags do
2+
@moduledoc """
3+
Updates resources based on their most recent snapshots.
4+
5+
This file was autogenerated with `mix ash_postgres.generate_migrations`
6+
"""
7+
8+
use Ecto.Migration
9+
10+
def up do
11+
alter table(:tags) do
12+
add(:title, :text)
13+
end
14+
end
15+
16+
def down do
17+
alter table(:tags) do
18+
remove(:title)
19+
end
20+
end
21+
end

test/aggregate_test.exs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,31 @@ defmodule AshSql.AggregateTest do
738738

739739
assert Enum.sort(user.years_visited) == ["1955", "1985", "1985", "2015"]
740740
end
741+
742+
test "reproduction of a bug where joins involving an aggregate use the wrong id on a join condition" do
743+
tag =
744+
AshPostgres.Test.Tag
745+
|> Ash.Changeset.for_create(:create, %{title: "Hello There"})
746+
|> Ash.create!()
747+
748+
post =
749+
AshPostgres.Test.Post
750+
|> Ash.Changeset.for_create(:create, %{title: "A Post"})
751+
|> Ash.create!()
752+
753+
comment =
754+
AshPostgres.Test.Comment
755+
|> Ash.Changeset.for_create(:create, %{title: "Hello There"})
756+
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
757+
|> Ash.create!()
758+
759+
_post_tag =
760+
AshPostgres.Test.PostTag
761+
|> Ash.Changeset.for_create(:create, %{post_id: post.id, tag_id: tag.id})
762+
|> Ash.create!()
763+
764+
assert Ash.calculate!(tag, :has_post_with_comment_with_same_title) == true
765+
end
741766
end
742767

743768
describe "first" do

test/support/resources/tag.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ defmodule AshPostgres.Test.Tag do
2525

2626
attributes do
2727
uuid_primary_key(:id)
28+
attribute(:title, :string, allow_nil?: true, public?: true)
2829
attribute(:importance, :integer, allow_nil?: false, default: 0, public?: true)
2930
end
3031

@@ -48,4 +49,12 @@ defmodule AshPostgres.Test.Tag do
4849
sort(score_after_winning: :desc)
4950
end
5051
end
52+
53+
calculations do
54+
calculate(
55+
:has_post_with_comment_with_same_title,
56+
:boolean,
57+
expr(title in posts.uniq_comment_titles)
58+
)
59+
end
5160
end

0 commit comments

Comments
 (0)