[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 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
[Rails] What to do when rails db: migrate cannot be done because there is no table referenced by the foreign key
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)
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
[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
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 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 ffi installation fails when launching an application in Rails
[Rails] What to do when the Refile image is not displayed when writing the processing at the time of Routing Error
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
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"
[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 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
What to do if you can't use the rails command
I added a Jar file to the build path in an Eclipse project, but the situation and what to do when `java.util.zip.ZipException: invalid LOC header (bad signature)` appears.
What to do when "relation" hibernate_sequence "does not exist" in the ID column of PostgreSQL + JPA
Throw an exception and catch when there is no handler corresponding to the path in spring
What to do when you want to know the source position where the method is defined in binding.pry
What to do if you get a JNI shared library error when trying to build in Eclipse
What to do if the app is not created with the latest Rails version installed when rails new
Method definition location Summary of how to check When defined in the project and Rails / Gem
What to do if an error occurs when doing npm install axios in React + Typescript project
What to do when javax.el.ELException: Not a Valid Method Expression: appears when the JSF screen is displayed
What to do when an UnsupportedCharsetException occurs in a lightweight JRE
What to do if you get a gcc error in Docker
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
[Rails] What to do when rails s does not respond or does not stop