gpt4 book ai didi

ruby-on-rails - PG::UndefinedTable: 错误: 表 "table-name"不存在

转载 作者:数据小太阳 更新时间:2023-10-29 08:03:30 24 4
gpt4 key购买 nike

我正在弄乱我的数据库,创建和删除表。将它们推送到 heroku 后,我删除了一些迁移文件。之前我创建了一个表“妈妈”。后来我想重命名它,所以我删除了'moms'并创建了新表'minutes_of_meetings'。我做了 $rake db:migrate,一切都成功完成,我的应用程序在本地主机上完美运行。

将其推送到 heroku 后,当我执行 $heroku 运行 rake db:migrate 时,它​​生成了以下日志:

  ActiveRecord::SchemaMigration Load (0.7ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to DropMoms (20150823142852)
(0.6ms) BEGIN
== 20150823142852 DropMoms: migrating =========================================
-- drop_table(:moms)
(0.9ms) DROP TABLE "moms"
PG::UndefinedTable: ERROR: table "moms" does not exist
: DROP TABLE "moms"
(0.5ms) ROLLBACK
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR: table "moms" does not exist
: DROP TABLE "moms"/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'

我在 heroku 中创建了一个新表“moms”,以便在迁移运行时可以将其删除。我这样做了:

$ heroku run Ruby console for rails-app-name >> ActiveRecord::Migration.create_table :moms 

我还创建了一个迁移来创建表“moms”。但错误仍然存​​在。

编辑:

这是我的 CreateMoms 迁移文件:

class CreateMoms < ActiveRecord::Migration
def change
create_table :moms do |t|
t.string :name
t.timestamp null: false
end
end
end

当我运行 heroku run rake db:migrate:up

Running `rake db:migrate:up` attached to terminal... up, run.1729
rake aborted!
VERSION is required
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:78:in `block (3 levels) in <top (required)>'
Tasks: TOP => db:migrate:up
(See full trace by running task with --trace)
WARNING: Toolbelt v3.41.3 update available.

在 heroku 上运行 rake db:migrate:down

Running `rake db:migrate:down` attached to terminal... up, run.6389
rake aborted!
VERSION is required - To go down one migration, run db:rollback
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:86:in `block (3 levels) in <top (required)>'
Tasks: TOP => db:migrate:down
(See full trace by running task with --trace)
WARNING: Toolbelt v3.41.3 update available.

最佳答案

注意

rake db:schema:load 将从 heroku 数据库中清除所有数据。请确保您在 heroku 上有数据备份。如果您还没有 heroku 数据库的备份,您可以使用 Heroku PGBackups 轻松备份。

看起来您的 schema 乱七八糟。只需使用 rake db:schema:load 将架构加载到数据库,然后再次运行迁移:

heroku run rake db:schema:load
heroku run rake db:migrate

关于ruby-on-rails - PG::UndefinedTable: 错误: 表 "table-name"不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32168182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com