{"id":30052542,"url":"https://github.com/jechol/ash_sql_has_one_sort_bug","last_synced_at":"2025-08-07T17:05:14.820Z","repository":{"id":306411283,"uuid":"1026085753","full_name":"jechol/ash_sql_has_one_sort_bug","owner":"jechol","description":null,"archived":false,"fork":false,"pushed_at":"2025-07-25T09:50:43.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-02T18:52:56.683Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jechol.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-25T09:30:52.000Z","updated_at":"2025-07-25T09:50:47.000Z","dependencies_parsed_at":"2025-07-25T15:51:03.551Z","dependency_job_id":"d1c9e396-a54e-455b-8899-b2ba3f14a87e","html_url":"https://github.com/jechol/ash_sql_has_one_sort_bug","commit_stats":null,"previous_names":["jechol/ash_sql_has_one_sort_bug"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jechol/ash_sql_has_one_sort_bug","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jechol%2Fash_sql_has_one_sort_bug","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jechol%2Fash_sql_has_one_sort_bug/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jechol%2Fash_sql_has_one_sort_bug/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jechol%2Fash_sql_has_one_sort_bug/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jechol","download_url":"https://codeload.github.com/jechol/ash_sql_has_one_sort_bug/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jechol%2Fash_sql_has_one_sort_bug/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269292917,"owners_count":24392507,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-07T02:00:09.698Z","response_time":73,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-08-07T17:05:13.076Z","updated_at":"2025-08-07T17:05:14.811Z","avatar_url":"https://github.com/jechol.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ash has_one no attribute + complex filter + sort bug\n\n## Symptoms\n\nPost has Tag relationships with both many_to_many and has_one, sorted by importance: :desc. While sorting works for many_to_many, it doesn't work for has_one.\n\n```elixir\n  relationships do\n    many_to_many :tags, MyDomain.Tag do\n      public? true\n      through MyDomain.PostTag\n      sort importance: :desc\n    end\n\n    has_one :most_important_tag, MyDomain.Tag do\n      public? true\n      no_attributes? true\n      filter expr(posts.id == parent(id))\n      sort importance: :desc\n    end\n  end\n```\n\n\n## Reproduction Method\n\n### Failing Test\nThere is a failing test that can be confirmed with\n```shell\n$ mix setup\n$ mix test\n```\n\n## Estimated Cause\n\nWhen beautifying the query displayed during `mix ecto.reset`, it looks like this:\n\n```sql\nSELECT DISTINCT\n\tp0.\"id\",\n\ts1.\"id\",\n\ts1.\"importance\",\n\ts1.\"__order__\"\nFROM\n\t\"public\".\"posts\" AS p0\n\tINNER JOIN LATERAL (\n\t\tSELECT DISTINCT\n\t\t\tON (st0.\"id\") st0.\"id\" AS \"id\",\n\t\t\tst0.\"importance\" AS \"importance\",\n\t\t\trow_number() OVER \"order\" AS \"__order__\"\n\t\tFROM\n\t\t\t\"public\".\"tags\" AS st0\n\t\t\tINNER JOIN \"public\".\"post_tags\" AS sp1 ON st0.\"id\" = sp1.\"tag_id\"\n\t\t\tINNER JOIN \"public\".\"posts\" AS sp2 ON sp2.\"id\" = sp1.\"post_id\"\n\t\tWHERE\n\t\t\t(sp2.\"id\"::bigint = p0.\"id\"::bigint::bigint)\n\t\tWINDOW\n\t\t\t\"order\" AS (\n\t\t\t\tORDER BY\n\t\t\t\t\tst0.\"importance\" DESC\n\t\t\t)\n\t\tORDER BY\n\t\t\tst0.\"id\",\n\t\t\tst0.\"importance\" DESC\n\t\tLIMIT\n\t\t\t1\n\t) AS s1 ON TRUE\nWHERE\n\t(p0.\"id\"::bigint = ANY ('{81}'::BIGINT[]))\nORDER BY\n\ts1.\"__order__\"\n```\n\nThe issue occurs because the unspecified id is placed first in the ORDER BY clause below.\n```sql\n\t\tORDER BY\n\t\t\tst0.\"id\",\n\t\t\tst0.\"importance\" DESC\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjechol%2Fash_sql_has_one_sort_bug","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjechol%2Fash_sql_has_one_sort_bug","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjechol%2Fash_sql_has_one_sort_bug/lists"}