Assumption
I am using docker
to create an application. The first time I tried to deploy it to Heroku, it was deploying fine, but I got an error on heroku run rails db:migrate
. I checked PG::UndefinedTable: ERROR: relation "users" does not exist
and found that the error was caused by the fact that other tables related to users were run first due to migration. I have tried everything, but I can't seem to solve the problem, so I am asking this question.
What we want to achieve
I want to resolve the error and reflect the ``db
Code
Migration File Structure
api/db/migrate
20210403234559_devise_token_auth_create_users.rb
20210626094529_create_posts.rb
20210626095339_create_post_items.rb
20220316090508_create_schedules.rb
20210403234559_devise_token_auth_create_users.rb
class DeviseTokenAuthCreateUsers < ActiveRecord::Migration[6.0]
def change
create_table(:users) do |t|
## Required
t.string :provider, :null => false, :default => "email"
t.string :uid, :null => false, :default => ""
## Database authenticatable
t.string :encrypted_password, :null => false, :default => ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
t.boolean :allow_password_change, :default => false
## Rememberable
t.datetime :remember_created_at
## Confirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email # Only if using reconfirmable
## Trackable
t.integer :sign_in_count, default: 0, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
## Lockable
# t.integer :failed_attempts, :default => 0, :null => false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
## User Info
t.string :name
t.string :nickname
t.string :image
t.string :email
t.boolean :guest
## Tokens
t.json :tokens
t.timestamps
end
add_index :users, :email, unique: true
add_index :users, [:uid, :provider], unique: true
add_index :users, :reset_password_token, unique: true
add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
end
20210626094529_create_posts.rb
class CreatePosts < ActiveRecord::Migration[6.0]
def change
create_table :posts do |t|
t.string :title
t.string :author
t.string :image
t.references :user, foreign_key: true
t.timestamps
end
end
end
20210626095339_create_post_items.rb
class CreatePostItems < ActiveRecord::Migration[6.0]
def change
create_table :post_items do |t|
t.string :content
t.references :post, foreign_key: true
t.boolean :status
t.timestamps
end
end
end
20220316090508_create_schedules.rb
class CreateSchedules < ActiveRecord::Migration[6.0]
def change
create_table :schedules do |t|
t.string :name
t.string :color
t.bigint :start
t.bigint :end
t.boolean :timed
t.boolean :long_time
t.integer :post_id
t.integer :long_term_id
t.references :user, foreign_key: true
t.timestamps
end
end
end
schema.rb
ActiveRecord::Schema.define(version: 2022_03_16_090508) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "post_items", force: :cascade do |t|
t.string "content"
t.bigint "post_id"
t.boolean "status"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["post_id"], name: "index_post_items_on_post_id"
end
create_table "posts", force: :cascade do |t|
t.string "title"
t.string "author"
t.string "image"
t.bigint "user_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["user_id"], name: "index_posts_on_user_id"
end
create_table "schedules", force: :cascade do |t|
t.string "name"
t.string "color"
t.bigint "start"
t.bigint "end"
t.boolean "timed"
t.boolean "long_time"
t.integer "post_id"
t.integer "long_term_id"
t.bigint "user_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["user_id"], name: "index_schedules_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "provider", default: "email", null: false
t.string "uid", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.boolean "allow_password_change", default: false
t.datetime "remember_created_at"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "name"
t.string "nickname"
t.string "image"
t.string "email"
t.boolean "guest"
t.json "tokens"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
t.index ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true
end
add_foreign_key "post_items", "posts"
add_foreign_key "posts", "users"
add_foreign_key "schedules", "users"
end
puma
workers Integer(ENV.fetch("WEB_CONCURRENCY") { 2 })
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
threads min_threads_count, max_threads_count
rackup DefaultRackup
port ENV.fetch("PORT") { 3000 }
preload_app!
on_worker_boot do
ActiveRecord::Base.establish_connection
end
Error
DEBUG -- : (18.5ms) CREATE TABLE "posts" ("id" bigserial primary key, "title" character varying, "author" character varying, "image" character varying, "user_id" bigint, "created_at" timestamp(6) NOT NULL, "updated_at" timestamp(6) NOT NULL, CONSTRAINT "fk_rails_5b5ddfd518"
FOREIGN KEY ("user_id")
REFERENCES "users" ("id")
)
DEBUG -- : (1.1ms) ROLLBACK
DEBUG -- : (1.3ms) SELECT pg_advisory_unlock(714769981607619105)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "users" does not exist
・
・
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "users" does not exist
・
・
PG::UndefinedTable: ERROR: relation "users" does not exist
What we tried
- Changed the date of
2022040403234559_devise_token_auth_create_users.rb
migration file to20210403234559_devise_token_auth_create_users.rb
. We then reset allDB
, did a migration, and started over.But the error remained the same.
database: app_development
Status Migration ID Migration Name
--------------------------------------------------
up 20210403234559 Devise token auth create users
up 20210626094529 Create posts
up 20210626095339 Create post items
up 20220316090508 Create schedules
- I wanted to reflect
20210403234559_devise_token_auth_create_users.rb
on Heroku, so I ranheroku run rails db:migrate VERSION=20210403234559_devise_token_auth_create_users.rb
. When I runtoken_auth_create_users.rb
, I get the following error
DEBUG -- : (1.9ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
DEBUG -- : (1.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
DEBUG -- : (1.1ms) SELECT pg_try_advisory_lock(714769981607619105)
DEBUG -- : (1.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
DEBUG -- : (1.1ms) SELECT pg_advisory_unlock(714769981607619105)
rails aborted!
ActiveRecord::UnknownMigrationVersionError:
No migration with version number 20210403234559.
Code executed by heroku
$ heroku login
$ heroku update beta
$ heroku plugins:install @heroku-cli/plugin-manifest
$ heroku create <App Name> --manifest
$ git push heroku master
$ heroku config:set RAILS_MASTER_KEY=<master key>
api $ heroku run rails db:migrate
Error
CodePudding user response:
Rename the file from 20210403234559_devise_token_auth_create_users.rb
to 00220403234559_devise_token_auth_create_users.rb
after a re-push I used the heroku run rails db:migrate
command and it worked.
20210403234559_devise_token_auth_create_users.rb
to00220403234559_devise_token_auth_create_users.rb
$ rails db:migrate:reset
$ git push
$ heroku run rails db:migrate
Thank you very much for all your help and advice.
CodePudding user response:
I think it will be useful to you. i did it
https://devcenter.heroku.com/articles/build-docker-images-heroku-yml