TalentBox / sequel-rails

A gem for using Sequel with Rails 5.x, 6.x, 7.x, 8.x
http://talentbox.github.io/sequel-rails/
MIT License
326 stars 81 forks source link

rails5 dbconsole cannot work: uninitialized constant Rails::DBConsole::ActiveRecord (NameError) #126

Closed gittmaekawa closed 7 years ago

gittmaekawa commented 7 years ago

I am an absolutely beginner to sequel and have tried sequel-rails. I encountered error as following. very confused, I have no idea. How can I solve this problem?

$ bundle exec rails db
/home/vagrant/work/sequel_test/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands/dbconsole.rb:146:in `configurations': uninitialized constant Rails::DBConsole::ActiveRecord (NameError)
        from /home/vagrant/work/sequel_test/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands/dbconsole.rb:131:in `config'
        from /home/vagrant/work/sequel_test/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands/dbconsole.rb:57:in `start'
        from /home/vagrant/work/sequel_test/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands/console_helper.rb:9:in `start'
        from /home/vagrant/work/sequel_test/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:100:in `dbconsole'
        from /home/vagrant/work/sequel_test/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
        from /home/vagrant/work/sequel_test/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands.rb:18:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

What I did before the error are:

$ bundle exec rails new . --skip-bundle --skip-git --skip-action-mailer --skip-active-record --skip-action-cable --skip-sprockets --skip-javascript --skip-turbolinks --api

$ vi Gemfile
# add 2 lines
gem 'sqlite3'
gem 'sequel-rails'

$ bundle install  --path=vendor/bundle

$ vi config/database.yml
development:
  adapter: sqlite # Also accept sqlite3
  database: db/mydatabase.sqlite # Path to db relative to Rails root

$ bundle exec rake db:create

$ bundle exec rails g model User
      invoke  sequel
      create    db/migrate/20170117030056_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml

$ vi db/migrate/20170117030056_create_users.rb
Sequel.migration do
  change do
    create_table :users do
      String :user_no, primary_key: true
      String :first_name
      String :last_name
    end
  end
end

$ bundle exec rake db:migrate

I also made sure if there are any the words 'active_record' in configuration files, but seemed there was nothing suspicious...

$ grep -nR active_record config
config/application.rb:7:# require "active_record/railtie"
gittmaekawa commented 7 years ago

I inspected the error and modified dbconsole's code then made it.

## railties-5.0.1/lib/rails/commands/dbconsole.rb

# in the method named 'config', change like this:
#raise ActiveRecord::AdapterNotSpecified, "'#{environment}' database is not configured. Available configuration: #{configurations.inspect}"
raise "'#{environment}' database is not configured. Available configuration: #{configurations.inspect}"

# in the method named 'configurations' , change as bellow:
#        ActiveRecord::Base.configurations = Rails.application.config.database_configuration
#        ActiveRecord::Base.configurations
        Rails.application.config.database_configuration

And If you use sqlite, use 'sqlite3 as adopter in database.yml adapter: sqlite3

DBConsole distinguishs databases by these words: mysql, postgres, postgis, sqlite3, oracle, oracle_enhanced, sqlserver,