[RUBY] (Solution) Mysql2 :: Error: Table'users' already exists

Error that occurred

In the flow of introducing devise, when I modify the migration file and execute rails db: migrate, the following error occurs.

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

After checking, it is necessary to modify encoding: utf8mb4 in database.yml to encoding: utf8. (Utf8mb4 uses 4 bytes per character and seems to exceed the character limit)

Therefore, if you modify it to encoding: utf8 and execute rails db: migrate again, a title error (below) will occur. Mysql2::Error: Table 'users' already exists

What was investigated and hypothesis

The Japanese translation of the error means that the users table already exists. This time, as a result of trying to execute rails db: migrate, the error is displayed, so it is probably already migrated. (= Ststus is up).

In other words, the hypothesis is that it can be solved by rolling back, deleting the users table once, and creating it again.

Hypothesis-based execution results

rails db:rollback rails d devise user rails g devise user Fix migration file rails db:migrate

→ Title error again (Mysql2 :: Error: Table'users' already exists)

I do not understand what it means. ..

Conclusion

As a result, it was resolved with rails db: reset. The reason for this was the existence of schema.rb.

This file manages the generation order of migrate files. And if you do not reset this file, even if you delete the table, it will be in the up state. So if you drop the table once and the error persists, you should keep reset in mind.

Note that another table will also be reset. You have to be careful.

Recommended Posts

(Solution) Mysql2 :: Error: Table'users' already exists
Mysql2 :: Error: Table'relationships' already exists
[Deployment error] Mysql2 :: Error: Table'~' already exists: CREATE TABLE `~`
MySql connection error
Resolve Mysql2 :: Error :: ConnectionError
(Solution) Mysql2 :: Error: Field'Column name' doesn't have a default value
[Rails] PG :: DuplicateTable: ERROR: relation "users" How to resolve already exists