ash-project / ash-project/ash

Expr produced invalid SQL instead of ash-error

Open
#1,865 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Elixir
Stars
2.5k
Forks
422
Avg merge
23h 26m
Merged PRs (30d)
46

Description

**Describe the bug**
Query:

```
def filter(actor, %{resource: resource}, _options)
when resource in [
Safari.Outcrop.Study
] do
expr(approved == true and ^actor.company_id in project.study_access.company_id)
end
```
Produces invalid sql:
```
SELECT s0."id", s0."__order__"
FROM (SELECT DISTINCT ON (ss0."id") ss0."id" AS "id", row_number() OVER "order" AS "__order__"
FROM "study" AS ss0
INNER JOIN "public"."project" AS sp1 ON ss0."project_id" = sp1."id"
INNER JOIN "public"."study_access" AS ss2 ON sp1."id" = ss2."project_id"
WHERE (ss0."approved"::boolean::boolean = true::boolean::boolean)
AND (60 = ANY (ss2."company_id"::bigint)) WINDOW "order" AS (ORDER BY ss0."id")
ORDER BY ss0."id") AS s0
ORDER BY s0."__order__"
LIMIT 9999
```

**To Reproduce**
See above

**Expected behavior**
Expect it to error out in Ash before reaching SQL layer

**Runtime**
- Ash main as of 15th of march

Contributor guide

Open the contributing guide

Research direction

Start with the Ash filter and expr(...) handling shown in the reproduction, then trace relationship traversal for project.study_access.company_id before SQL generation. Reproduce the query and inspect where validation should occur; done means Ash raises an error before emitting invalid SQL.

Written by the indexing model from the issue text.

Assessment

Tech stack
elixir
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.