We've moved discussions to Discord

Strange issue when seeding users with a rake task?

Aska Konomi
Hi! I run into a strange issue when trying to create users in `seeds.rb`.

I have a rake task like this:
if Rails.env.development?
  Rake::Task['db:drop'].execute
  Rake::Task['db:create'].execute
end
Rake::Task['db:migrate'].execute
Rake::Task['db:seed'].execute
And the content in the seeds.rb file:
User.create(
  name: "Test Admin", email: "admin@user.com",
  password: password, password_confirmation: password,
  admin: true, terms_of_service: true
)
But when I run the task, this error happened:
Caused by:
PG::UndefinedColumn: ERROR:  column "processor" of relation "accounts" does not exist
LINE 1: ...wner_id", "personal", "created_at", "updated_at", "processor...
                                                             ^
/Users/.../app/models/concerns/user_accounts.rb:29:in `create_default_account'
/Users/.../db/seeds.rb:2:in `<main>'
/Users/.../lib/tasks/samples.rake:8:in `block in <main>'
/Users/.../.rbenv/versions/3.0.3/bin/bundle:23:in `load'
/Users/.../.rbenv/versions/3.0.3/bin/bundle:23:in `<main>'
Tasks: TOP => create_samples
I guess the "processor" attribute has something to do with the account users?

This is a fresh created JSP project with the Rails 7 update, I didn't modify anything in the user or account model/migration/etc.

The strange part is if I run user create in the rails console, or in the console by running
rails db:seed
# or
bundle exec rake db:seed
The seed file works fine.

What could be the issue?

Update

I think the issue is when the seed task is invoked, the migration task is not fully completed yet. I was able to fix the issue with the enhance method like this:

After more digging, this is the issue:
https://stackoverflow.com/questions/37850322/rails-dbseed-unknown-attribute

And I had to add these lines to the seed file to make it work:
User.reset_column_information
Account.reset_column_information
AccountUser.reset_column_information
Tabish Iqbal
You need to create an account, account_user records as well and associate the user to those.
Aska Konomi
Tabish Iqbal  
I thought the default account and account_user should be created automatically by the `create_default_account` method? As in the user_accounts concern:
after_create :create_default_account, unless: :skip_default_account?
I think the issue is when the seed task is invoked, the migration task is not fully completed yet. I was able to fix the issue with the enhance method like this:

After more digging, this is the issue:
https://stackoverflow.com/questions/37850322/rails-dbseed-unknown-attribute

And I had to add these lines to the seed file to make it work:
User.reset_column_information
Account.reset_column_information
AccountUser.reset_column_information
Anyways, thank you for the help!
Porter Bayne
Thank you! I was running into this just now and you saved me time & frustration!
Notifications
You’re not receiving notifications from this thread.