假設
我正在使用docker創建應用程式。我第一次嘗試將它部署到 Heroku 時,它部署得很好,但我在heroku run rails db:migrate. 查了一下PG::UndefinedTable: ERROR: relation "users" does not exist,發現這個錯誤是因為遷移導致其他與用戶相關的表先運行導致的。我已經嘗試了一切,但我似乎無法解決問題,所以我問了這個問題。
我們想要達到的目標
我想解決錯誤并反映 ``db
代碼
遷移檔案結構
api/db/遷移
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
架構.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
彪馬
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
錯誤
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
我們嘗試了什么
2022040403234559_devise_token_auth_create_users.rb將遷移檔案的日期更改為20210403234559_devise_token_auth_create_users.rb. 然后我們重置所有DB,進行遷移,然后重新開始。但錯誤仍然存??在。
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
- 我想反思
20210403234559_devise_token_auth_create_users.rbHeroku,所以我跑了heroku run rails db:migrate VERSION=20210403234559_devise_token_auth_create_users.rb。當我運行時token_auth_create_users.rb,我收到以下錯誤
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.
執行的代碼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錯誤
uj5u.com熱心網友回復:
重新推送后將檔案從 重命名為 ,20210403234559_devise_token_auth_create_users.rb我使用了該命令并且它有效。00220403234559_devise_token_auth_create_users.rbheroku run rails db:migrate
20210403234559_devise_token_auth_create_users.rb到00220403234559_devise_token_auth_create_users.rb$ rails db:migrate:reset$ git push$ heroku run rails db:migrate
非常感謝您的所有幫助和建議。
uj5u.com熱心網友回復:
我想這對你會有用。我做到了
https://devcenter.heroku.com/articles/build-docker-images-heroku-yml
uj5u.com熱心網友回復:
Rails 使用該schema_migrations表以時間戳作為版本號來跟蹤遷移。
你可以做兩件事來修復這個故障。
- 洗掉
20210403234559并重schema_migrations試。 - 重命名
20210403234559為其他時間戳。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/472682.html
標籤:轨道上的红宝石 红宝石 PostgreSQL 码头工人 heroku
