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?
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 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 ^^