[RUBY] [Rails] What to do when rails db: migrate cannot be done because there is no table referenced by the foreign key

Error location

rails aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Table 'first_app_development.users' doesn't exist

There is no user table to reference! Is angry. Why is this happening?

Conclusion

The files in the users table that the messages table wants to refer to have been generated after the messages table.

**what do you mean? ** **

rails g model:message

After the

rails g model:user

Did you cause the error? Normally, this order should be reversed. Since user: message has a one-to-many relationship, the message must refer to the user.

_create_messages.rb


class CreateMessages < ActiveRecord::Migration[6.0]
  def change
    create_table :messages do |t|
      t.string :image
      t.references :user, foreign_key: true
      t.timestamps
    end
  end
end

In this way, if you add foreign_key: true to the column and write "I will refer to the user table!", It means that an error will occur unless the referenced user table is migrated first.

** Then how do I change the order of the files? ** ** The answer is simple. Of the migration file e1347bffe8989928198807e72930578c.png Just right-click on the date order and edit the file name to change it.

Replaced and again

rails db:migrate
== 20200814114004 CreateUsers: migrating ======================================
-- create_table(:users)
   -> 0.1215s
== 20200814114004 CreateUsers: migrated (0.1216s) =============================

== 20200815114016 CreateMessages: migrating ===================================
-- create_table(:messages)
   -> 0.0479s
== 20200815114016 CreateMessages: migrated (0.0480s) =========================

It's done ^^

Recommended Posts

[Rails] What to do when rails db: migrate cannot be done because there is no table referenced by the foreign key
[Rails] What to do when the error No database selected and Unknown database appears in db: migrate
[React.useRef] What to do when the latest state cannot be referenced in the event listener
[Rails] What to do when you want to generate an error that cannot be destory when foreign key restrictions are applied
What to do when rails db: seed does not reflect in the database
What to do when rbenv says that there is no readline or lib history
% rails db: When creating, the LoadError caused by mimemagic is
# What to do if you accidentally do rails db: migrate: drop
What to do if the adb command cannot be executed
[Rails] What to do when the view collapses when a message is displayed with the errors method
What to do about the "cannot be read or is not a valid ZIP file" error
What to do if the app is not created with the latest Rails version installed when rails new
[Rails] What to do if data is not registered in DB
What to do if Cloud9 is full in the Rails tutorial
The story that I struggled because I couldn't do "Rails db: migrate".
Rails tutorial When rails new cannot be done due to different versions
[Rails / Docker] What to do if access is denied by the browser (localhost: 3000) after adding a gem
[IOS] What to do when the image is filled with one color
What to do when CentOS cannot be started with VirtualBox on Catalina
What to do when the error "Non-static variable x cannot be referenced from static context" in Java. Lessons from the very first step in programming Remarks 01
What to do if the build fails with the error "Module compiled with Swift x.x.x cannot be imported by the Swift x.x.x compiler"
[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
[Rails] [Parent-child relationship] I want to register the foreign key in the child with nil when the parent is deleted.
[Rails] I want to reset everything because the data in the local environment is strange! What to do before that
What to do when you want to delete a migration file that is "NO FILE"
What to do if SPA URL loading cannot be detected by WebView shouldOverrideUrlLoading
What to do when is invalid because it does not start with a'-'
[Ubuntu] What to do when MongoDB cannot be started due to a SocketException error
[Rails] Bootstrap's text-light cannot be inherited by the link_to method because the class is not written as an argument in the method.
How to solve when you cannot connect to DB with a new container because the port is assigned to the existing docker container
After all, what is [rails db: migrate] doing?
After installing'devise''bootstrap' of gemfile with rails, what to do when url is an error
What to do when "Fail to load the JNI shared library" is displayed in Eclipse
What to do when Address already in use is displayed after executing rails s
[Ruby on Rails] How to stop when Rails server cannot be stopped by Ctrl + C
What to do when an error occurs in rails db: migrate ((StandardError: An error has occurred, this and all later migrations canceled :))
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 if the Rails server can't start
What to do when The SSL certificate has expired
What to do if rails server can't be stopped
What to do when rails creates a 〇〇 2.rb file
ParseException: What to do when Unparseable date is reached
[Joke] What to do when the invalid sample rate is reached when using Mixxx on Ubuntu 20.04
What to do when Git Repository cannot be displayed in Team Explorer for Eclipse in Azure
What to do when Rails on Docker does not reflect controller changes in the browser