luckyframework / luckyframework/avram

Support write LEFT JOIN which include more than one condition use AND?

Open
#1,048 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Crystal
Stars
183
Forks
67
PR merge metrics
No merged PRs in 30d

Description

For discusstion, check [#1901](https://github.com/luckyframework/lucky/discussions/1901) and optional [this](https://github.com/luckyframework/lucky/discussions/1893)

Following is a SQL example which i expected to generate.

university has_many chong_wen_baos
chong_wen_bao belongs_to university

```sql
SELECT universities.* FROM universities
LEFT JOIN chong_wen_baos
ON
universities.id = chong_wen_baos.university_id
AND
chong_wen_baos.user_id = 1;
```

As you can see, i use a AND condition with the LEFT JOIN ON, Above SQL output expected result, which includes all university, whatever whether there is record exists on the right table (the select chong_wen_baos.id is null if no right table record exists)

I don't know how to write SQL like above, current, I am using like this, but not work.

```crystal
query = query
.where_chong_wen_baos(ChongWenBao.new.user_id(current_user.id), auto_inner_join: false)
.left_join_chong_wen_baos
```

It generate SQL like this: (AND replaced with WHERE)

```sql
SELECT universities.* FROM universities
LEFT JOIN chong_wen_baos
ON universities.id = chong_wen_baos.university_id
WHERE
chong_wen_baos.user_id = 1;
```

this is not what i want, because it is not return the left table records which no right table record exists.

I thought one of solution is, write join like the `where` method?

```crystal
query = UniversityQuery.new
query.join("LEFT JOIN chong_wen_baos on universities.id = chong_wen_baos.university_id AND chong_wen_bao.user_id = ?", current_user.id)
```

Thanks.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with discussions #1901 and #1893, then trace how where_chong_wen_baos and left_join_chong_wen_baos generate their SQL. Define the supported join-condition API and verify it keeps the condition in LEFT JOIN ... ON, preserves universities without matching rows, and handles the shown user_id case.

Written by the indexing model from the issue text.

Assessment

Tech stack
crystal, postgresql
Domain
backend, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.