luckyframework / luckyframework/avram

Associations don't respect namespaces when defining PrimaryKeyType

Open
#463 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Given:

```crystal
module MyApp
class Post < BaseModel
table do
belongs_to user : User
end
end
end
```

If I try to define a SaveOperation for this:

```crystal
class SavePost < MyApp::Post::SaveOperation
#... blah
end
```

I'll get the error:

```
web | > 32 | user_id : User::PrimaryKeyType | Nothing = Nothing.new,
web | ^
web | Error: undefined constant User::PrimaryKeyType
```

The current work-around is if your model is namespaced, then the associations must also be defined with the namespace.

```crystal
module MyApp
class Post < BaseModel
table do
belongs_to user : MyApp::User
end
end
end
```

*EDIT*: ok, so doing that "work-around" actually doesn't quite work. It does let me make my save operation, but my queries are borked with `undefined method where_posts for MyApp::User`. Without looking, I'm assuming adding the namespace changes this...

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 by reproducing the namespaced MyApp::Post and MyApp::User association examples, then trace how SaveOperation generates User::PrimaryKeyType and how queries generate where_posts. Done means namespaced associations compile without manual qualification and the resulting queries work correctly.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.