[RUBY] [Rails] What to do when the error No database selected and Unknown database appears in db: migrate

Introduction

I started creating web services today because I wanted to put the ideas I came up with into shape. Immediately, I was addicted to the error, so I will leave it as a memorandum.

environment


Rails : 6.0.3.2
ruby  : 2.6.3

error contents

I am creating a user login screen with devise. An error occurred with the following command.

Terminal


$ rake db:migrate

rake aborted!
ActiveRecord::StatementInvalid: Mysql2::Error: No database selected

Apparently, the database has not been selected. If you check database.yml, ...

database.yml


default: &default
   adapter: mysql2
   encoding: utf8
   pool: 5
   username: <%= ENV['DATABASE_DEV_USER'] %>
   password: <%= ENV['DATABASE_DEV_PASSWORD'] %>
   host: <%= ENV['DATABASE_DEV_HOST'] %>

development:
   <<: *default
   database: <%= ENV['DATABASE_DEV_NAME'] %>

test:
   <<: *default
   database: <%= ENV['DATABASE_DEV_NAME'] %>

production:
   <<: *default
   database: <%= ENV['DATABASE_DEV_NAME'] %>

database is specified by an environment variable, It seems that an error occurred because there is nothing in the .env file.

Write database in database.yml directly instead of environment variables.

database.yml


default: &default
   adapter: mysql2
   encoding: utf8
   pool: 5
   username: <%= ENV['DATABASE_DEV_USER'] %>
   password: <%= ENV['DATABASE_DEV_PASSWORD'] %>
   host: <%= ENV['DATABASE_DEV_HOST'] %>

development:
   <<: *default
   database: development

test:
   <<: *default
   database: test

production:
   <<: *default
   database: production

Run rake db: migrate again.

Terminal


rake aborted!
ActiveRecord::NoDatabaseError: Unknown database 'development'

Is displayed. Apparently the database cannot be found.

Create database with rails & migrate (MySQL) I referred to the above article.

###Database creation
rake db:create:all

###migration
rake db:migrate

This completes successfully.

Recommended Posts

[Rails] What to do when the error No database selected and Unknown database appears in db: migrate
What to do when rails db: seed does not reflect in the database
What to do when an error (StandardError: An error has occurred, this and all later migrations canceled:) appears in rails db: migrate
What to do when an error occurs in rails db: migrate ((StandardError: An error has occurred, this and all later migrations canceled :))
[Programming beginner] What to do when rails s becomes an error in the local development environment
What to do when Blocked Host: "host name" appears in Ruby on Rails
What to do and how to install when an error occurs in DXRuby 1.4.7
[Rails] What to do when rails db: migrate cannot be done because there is no table referenced by the foreign key
Error in rails db: migrate
# What to do if you accidentally do rails db: migrate: drop
What to do when ‘Could not find’ in any of the sources appears in the development environment with Docker × Rails × RSpec
About the error that occurred when adding the column name in rails (rails db: migrate, rails db: rollback, add)
What to do when a could not find driver appears when connecting to a DB in a Docker environment
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
What to do when Rails on Docker does not reflect controller changes in the browser
[Rails] What to do if data is not registered in DB
What to do when the changes in the Servlet are not reflected
What to do if the Rails page doesn't appear in Rails tutorial 1.3.2
What to do if Cloud9 is full in the Rails tutorial
How to solve the unknown error when using slf4j in Java
[Ruby] What to do when the error "cannot load such file" appears when executing VS Code debug
What to do if you get an error during rails db: reset
I want to display an error message when registering in the database
What to do when "call'Hoge.connection' to establish a connection" appears on rails c
What to do if the image posted by refile disappears after setting a 404 error page in Rails
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
What to do when IllegalStateException occurs in PlayFramework
What to do when the value becomes null in the second getSubmittedValue () in JSF Validator
[Grails] Error occurred running What to do when the Grails CLI does not start
What to do if you get an error on heroku rake db: migrate
What to do if ffi installation fails when launching an application in Rails
What to do if you get an "A server is already running." Error when you try to start the rails server
[React.useRef] What to do when the latest state cannot be referenced in the event listener
After installing'devise''bootstrap' of gemfile with rails, what to do when url is an error
[In team development] Error when moving to another member's branch and doing rails s [Rails]
What to do if the Rails server can't start
What to do when The SSL certificate has expired
[Rails] "private method` String' called ~ "error when db: migrate
What to do when a null byte error occurs
What to do when rails creates a 〇〇 2.rb file
What to do when "Fail to load the JNI shared library" is displayed in Eclipse
What to do if Failure / Error: require File.expand_path ('../ config / environment', __dir__) appears in RSpec
What to do when Address already in use is displayed after executing rails s
Error ExecJS :: RuntimeUnavailable: What to do when it occurs
[Java] What to do if the contents saved in the DB and the name of the enum are different in the enum that reflects the DB definition
What to do if you get the warning "Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1." in Rails 6.0
What to do if you select a JRE in Eclipse and get "The selected JRE does not support the current compliance level 11"
What to do if you don't see the test code error message in the terminal console
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
How to solve the problem when the value is not sent when the form is disabled in rails and sent
[Ruby / Rails] What to do when NoMethodError appears when using a destructive method such as filter!
[Rails] What to do if you can't get an error message with the errors method
What to do if you get a "302" error in your controller unit test code in Rails
[Rails] What to do when the view collapses when a message is displayed with the errors method
What to do if you get an error in Basic authentication during Rails test code
[Rails 6] What to do when a missing a template error occurs after introducing haml [Super easy]
What to do when Method not found in f: ajax
What to do when you launch an application with rails
What to check when rails db: migration does not pass
PG :: DatatypeMismatch error when doing heroku run rails db: migrate
[Rails] How to display information stored in the database in view