rails db: migrate failed!

Error when running rails db: migrate

When you run rails db: migrate I got the following error!

StandardError: An error has occurred, all later migrations canceled:

Translated literally, it means that all subsequent migrations were canceled due to an error.

In addition, I am getting this error.

Mysql2::Error: Specified key was too long; max key length is 767 bytes

This error seems to occur because the specified key is too long. The key length of max seems to be 767 bytes.

Hypothesis of this error

The key length is max 767 bytes, but it seems that an error has occurred because the key is too long.

For the 255-character VARCHER type generated by rails by default

◎ ** utf8mb4 is 4 bytes per character **, 255 x 4 bytes = 1020 bytes   An error will occur because it will exceed 767 bytes.

Solution

Change the character code specification to ** utf8 **

◎ utf8 is 3 bytes per character, 255 x 3 bytes = 765 bytes

It fits almost exactly, so this is the solution!

database.yml



   default: &default
   encoding: utf8
   

Do bundle install and reboot with rails s!

afterwards,

$ rails db:migrate:reset

You have successfully migrated!

Recommended Posts

rails db: migrate failed!
Error in rails db: migrate
[Rails] Modify migrate file (rails db: rollback)
rails db: 〇〇 Summary
[Rails] Modify migrate file (rails db: rollback STEP =)
[heroku] run rails db: migrate doesn't work
[Rails] rails db command summary
After all, what is [rails db: migrate] doing?
rails test db only drop
Error with db: migrate: reset
# What to do if you accidentally do rails db: migrate: drop
PG :: DatatypeMismatch error when doing heroku run rails db: migrate
[rails] How to display db information
[Personal note] Rails DB index consideration
[Rails] DB design for EC site
The story that I struggled because I couldn't do "Rails db: migrate".
Rails DB PostgreSQL changed to MySQL
[Rails] Reflection to db using seeds.rb
Migrate existing Rails 6 apps to Docker environment
ArgumentError: Unknown validator:'UniqunessValidator' in rake db: migrate
[Rails] Store only checked items in DB