luckyframework / luckyframework/avram

Adding a column followed by an immediate query causes a column missing error

Open
#418 0 comments 1 reaction 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

Hey everyone,

I stumbled upon this issue when trying to fill a new non-nullable column with a dynamic value. According to the [guides](https://luckyframework.org/guides/database/migrations#using-fill_existing_with-and-default-values), this should have worked but it didn't:

```crystal
class AddDueDateToRecurrings::V20200704015059 < Avram::Migrator::Migration::V1
def migrate
alter table_for(Recurring) do
add due_date : Time?
end

RecurringQuery.new.each do |recurring|
SaveRecurring.update!(
recurring,
due_date: recurring.start_date.shift(months: recurring.count)
)

make_required table_for(Recurring), :due_date
end

def rollback
alter table_for(Recurring) do
remove :due_date
end
end
end
```

This code is almost exactly the same as the example code in the guides, but I got a "column not found" error when the query is executed:

```
$ lucky db.migrate
▸ Query SELECT recurrings.id, recurrings.created_at, recurrings.updated_at, recurrings.description, recurrings.amount, recurrings.start_date, recurrings.last_date, recurrings.repeats, recurrings.count, recurrings.last_payed_on, recurrings.due_date, recurrings.user_id, recurrings.category_id FROM recurrings. Args []
Unhandled exception: Unexpected error while running migrations: column recurrings.due_date does not exist (Exception)
from lib/avram/src/avram/migrator/migrator.cr:7:5 in 'call'
from lib/avram/src/avram/tasks/db/migrate.cr:3:1 in 'print_help_or_call'
from lib/lucky_cli/src/lucky_cli/runner.cr:27:9 in 'run'
from tasks.cr:20:1 in '__crystal_main'
from ../../../../../../usr/local/Cellar/crystal/0.35.1_1/src/crystal/main.cr:105:5 in 'main_user_code'
from ../../../../../../usr/local/Cellar/crystal/0.35.1_1/src/crystal/main.cr:91:7 in 'main'
from ../../../../../../usr/local/Cellar/crystal/0.35.1_1/src/crystal/main.cr:114:3 in 'main'
```

As a workaround, I ended up splitting the above into three separate migrations and it worked. Sadly, I have no further insights into this issue :/

```
Crystal version: 0.35.1
Lucky version: 0.23.0
```

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 example with `lucky db.migrate`, then read the migration path around `lib/avram/src/avram/migrator/migrator.cr` and the entry point in `lib/avram/src/avram/tasks/db/migrate.cr`. Done means adding a column and querying the model in the same migration no longer raises a column-missing error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.