luckyframework / luckyframework/avram

Issue preloading soft deleted records

Open
#672 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I'll need to investigate this more, but we just ran in to an issue with some preloads.

Take this model setup

```crystal
class User < BaseModel
table do
# ...
column soft_deleted_at : Time?
has_many transactions : Transaction
end
end

class Transaction < BaseModel
table do
belongs_to user : User
end
end

class User::BaseQuery
include Avram::SoftDelete::Query

def initialize
defaults &.only_kept
end
end

class UserQuery < User::BaseQuery
end

class TransactionQuery < Transaction::BaseQuery
end
```

Then we had some code like this:

```crystal
# in the action
transactions = TransactionQuery.new.preload_user

# in the page

transactions.each do |transaction|
td do
link(transaction.user.username, to: Users::Show.with(transaction.user_id))
end
end
```

We kept getting a 500 error saying `NilAssertionError` which generally relates to calling `not_nil!` somewhere. In this case, our `user` should never be nil, but calling `transaction.user` says otherwise. We printed out the record, and saw this:

```
#
```

Looking up user 4, we saw the record existed, but it was soft deleted.

Avram should probably log some sort of warning that a required record could not be preloaded, or maybe when you preload an association that's been soft deleted, that record isn't returned at all?

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 TransactionQuery.new.preload_user, transaction.user, and the Avram::SoftDelete::Query behavior shown in the report. Reproduce the preload of a soft-deleted User and trace how @_user_preloaded=true with @_preloaded_user=nil is handled. Done means the project has a decided and tested behavior for required associations whose records are soft deleted.

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.