Deploying Rails App to Heroku: ERROR: relation "todo_lists" does not exist
Deploying Rails App to Heroku: ERROR: relation "todo_lists" does not exist
几天来我一直被这个问题困扰。你们能提供一些指导来解决这个问题并将数据库部署到 Heroku 吗?
这是我的一部分 schema.rb
:
create_table "todo_items", force: :cascade do |t|
t.string "title"
t.bigint "user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.boolean "completed", default: false
t.datetime "completed_at"
t.bigint "todo_id", null: false
t.index ["todo_id"], name: "index_todo_items_on_todo_id"
t.index ["user_id"], name: "index_todo_items_on_user_id"
end
create_table "todos", force: :cascade do |t|
t.string "title"
t.bigint "user_id", null: false
t.bigint "project_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["project_id"], name: "index_todos_on_project_id"
t.index ["user_id"], name: "index_todos_on_user_id"
end
我正在尝试将新的 Rails 6 应用程序部署到 Heroku 并已完成以下操作:
heroku login
heroku create app
heroku addons:create heroku-postgresql:hobby-dev
git push heroku master
heroku run rails db:migrate
当我 运行 最后一个命令 heroku run rails db:migrate
- 我收到以下错误消息:
注意:我曾经创建了错误的 todo_list
引用和 table,后来删除了它们,因此 todo_lists
在我的模式中根本不存在。
错误消息 #1
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "todo_lists" does not exist
...
/app/db/migrate/20200606053040_create_todo_items.rb:3:in `change'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/transactions.rb:212:in `transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1361:in `ddl_transaction'
...
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/transactions.rb:212:in `transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1361:in `ddl_transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1309:in `execute_migration_in_transaction'
错误消息#2
Caused by:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "todo_lists" does not exist
...
/app/db/migrate/20200606053040_create_todo_items.rb:3:in `change'
...
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1002:in `migrate'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1310:in `block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1361:in `block in ddl_transaction'
...
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/transactions.rb:212:in `transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1361:in `ddl_transaction'
错误消息 #3
Caused by:
PG::UndefinedTable: ERROR: relation "todo_lists" does not exist
...
/app/db/migrate/20200606053040_create_todo_items.rb:3:in `change'
...
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1002:in `migrate'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1310:in `block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1361:in `block in ddl_transaction'
...
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:280:in `transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/transactions.rb:212:in `transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1361:in `ddl_transaction'
这是我的部分迁移 (rails db:migrate:status
):
up 20200604053157 Create todos
up 20200606053040 Create todo items
up 20200606235328 Add completed to todo items
up 20200606235537 Add completed at to todo items
up 20200608050430 Remove todo list id from todo items
up 20200608050957 Drop todo list
up 20200608051132 Add todo id to todo items
20200606053040_create_todo_items.rb
class CreateTodoItems < ActiveRecord::Migration[6.0]
def change
create_table :todo_items do |t|
t.string :title
t.references :user, null: false, foreign_key: true
t.references :todo_list, null: false, foreign_key: true
t.timestamps
end
end
end
20200608050430_remove_todo_list_id_from_todo_items.rb
class RemoveTodoListIdFromTodoItems < ActiveRecord::Migration[6.0]
def change
safety_assured { remove_reference :todo_items, :todo_list, null: false, foreign_key: true }
end
end
20200608050957_drop_todo_list.rb
class DropTodoList < ActiveRecord::Migration[6.0]
def change
drop_table :todo_lists
end
end
20200608051132_add_todo_id_to_todo_items.rb
class AddTodoIdToTodoItems < ActiveRecord::Migration[6.0]
disable_ddl_transaction!
def change
add_reference :todo_items, :todo, null: false, index: {algorithm: :concurrently}
end
end
我使用了这个命令并且有效:
heroku run rake db:migrate.
我记得一个类似的问题,我认为这就是答案。
https://devcenter.heroku.com/articles/getting-started-with-ruby?singlepage=true
好的,我猜下面的成功迁移发生在您的开发环境中:
up 20200604053157 Create todos
up 20200606053040 Create todo items
up 20200606235328 Add completed to todo items
up 20200606235537 Add completed at to todo items
up 20200608050430 Remove todo list id from todo items
up 20200608050957 Drop todo list
up 20200608051132 Add todo id to todo items
第二次迁移也确实引用了 todo_lists :
class CreateTodoItems < ActiveRecord::Migration[6.0]
def change
create_table :todo_items do |t|
t.string :title
t.references :user, null: false, foreign_key: true
t.references :todo_list, null: false, foreign_key: true
t.timestamps
end
end
end
但是在第二次迁移引用 todo_lists
之前,我只看到一个迁移 20200604053157 Create todos
,它可能对 todo_lists
没有任何作用
所以第一次和第二次迁移之间可能存在迁移,这导致了 todo_lists
table。但是你可能已经删除了这个文件。
开发中没有问题,因为这个文件曾经存在并且执行了todo_lists
table创建。但是,如果您删除此文件并要求 Heroku 上的 Rails 应用引用它,它就不能再这样做了。
然后你得到一个错误。
永远不要删除任何迁移文件。因为您逐步执行更改(新的 tables,销毁 tables..),所以您应该保持一切不变。或者以后的部署(和数据库创建)无法再执行您要求他们执行的操作。
现在解决这个问题:如果您还有原始文件的副本,只需恢复它即可。否则只需删除(或编辑)任何提到 todo_lists
的迁移,无论是创建还是销毁,然后重置数据库并重新创建它。 (好像是个项目然后我估计没什么大不了的)
这次todo_lists
不会知道
我从我的迁移文件中删除了对 todo_list
或 todo_lists
的任何提及或引用,并提交了我的代码。
然后运行以下内容:
heroku run rails db:migrate
几天来我一直被这个问题困扰。你们能提供一些指导来解决这个问题并将数据库部署到 Heroku 吗?
这是我的一部分 schema.rb
:
create_table "todo_items", force: :cascade do |t|
t.string "title"
t.bigint "user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.boolean "completed", default: false
t.datetime "completed_at"
t.bigint "todo_id", null: false
t.index ["todo_id"], name: "index_todo_items_on_todo_id"
t.index ["user_id"], name: "index_todo_items_on_user_id"
end
create_table "todos", force: :cascade do |t|
t.string "title"
t.bigint "user_id", null: false
t.bigint "project_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["project_id"], name: "index_todos_on_project_id"
t.index ["user_id"], name: "index_todos_on_user_id"
end
我正在尝试将新的 Rails 6 应用程序部署到 Heroku 并已完成以下操作:
heroku login
heroku create app
heroku addons:create heroku-postgresql:hobby-dev
git push heroku master
heroku run rails db:migrate
当我 运行 最后一个命令 heroku run rails db:migrate
- 我收到以下错误消息:
注意:我曾经创建了错误的 todo_list
引用和 table,后来删除了它们,因此 todo_lists
在我的模式中根本不存在。
错误消息 #1
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "todo_lists" does not exist
...
/app/db/migrate/20200606053040_create_todo_items.rb:3:in `change'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/transactions.rb:212:in `transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1361:in `ddl_transaction'
...
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/transactions.rb:212:in `transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1361:in `ddl_transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1309:in `execute_migration_in_transaction'
错误消息#2
Caused by:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "todo_lists" does not exist
...
/app/db/migrate/20200606053040_create_todo_items.rb:3:in `change'
...
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1002:in `migrate'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1310:in `block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1361:in `block in ddl_transaction'
...
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/transactions.rb:212:in `transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1361:in `ddl_transaction'
错误消息 #3
Caused by:
PG::UndefinedTable: ERROR: relation "todo_lists" does not exist
...
/app/db/migrate/20200606053040_create_todo_items.rb:3:in `change'
...
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1002:in `migrate'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1310:in `block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1361:in `block in ddl_transaction'
...
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:280:in `transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/transactions.rb:212:in `transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1361:in `ddl_transaction'
这是我的部分迁移 (rails db:migrate:status
):
up 20200604053157 Create todos
up 20200606053040 Create todo items
up 20200606235328 Add completed to todo items
up 20200606235537 Add completed at to todo items
up 20200608050430 Remove todo list id from todo items
up 20200608050957 Drop todo list
up 20200608051132 Add todo id to todo items
20200606053040_create_todo_items.rb
class CreateTodoItems < ActiveRecord::Migration[6.0]
def change
create_table :todo_items do |t|
t.string :title
t.references :user, null: false, foreign_key: true
t.references :todo_list, null: false, foreign_key: true
t.timestamps
end
end
end
20200608050430_remove_todo_list_id_from_todo_items.rb
class RemoveTodoListIdFromTodoItems < ActiveRecord::Migration[6.0]
def change
safety_assured { remove_reference :todo_items, :todo_list, null: false, foreign_key: true }
end
end
20200608050957_drop_todo_list.rb
class DropTodoList < ActiveRecord::Migration[6.0]
def change
drop_table :todo_lists
end
end
20200608051132_add_todo_id_to_todo_items.rb
class AddTodoIdToTodoItems < ActiveRecord::Migration[6.0]
disable_ddl_transaction!
def change
add_reference :todo_items, :todo, null: false, index: {algorithm: :concurrently}
end
end
我使用了这个命令并且有效:
heroku run rake db:migrate.
我记得一个类似的问题,我认为这就是答案。
https://devcenter.heroku.com/articles/getting-started-with-ruby?singlepage=true
好的,我猜下面的成功迁移发生在您的开发环境中:
up 20200604053157 Create todos
up 20200606053040 Create todo items
up 20200606235328 Add completed to todo items
up 20200606235537 Add completed at to todo items
up 20200608050430 Remove todo list id from todo items
up 20200608050957 Drop todo list
up 20200608051132 Add todo id to todo items
第二次迁移也确实引用了 todo_lists :
class CreateTodoItems < ActiveRecord::Migration[6.0]
def change
create_table :todo_items do |t|
t.string :title
t.references :user, null: false, foreign_key: true
t.references :todo_list, null: false, foreign_key: true
t.timestamps
end
end
end
但是在第二次迁移引用 todo_lists
之前,我只看到一个迁移 20200604053157 Create todos
,它可能对 todo_lists
所以第一次和第二次迁移之间可能存在迁移,这导致了 todo_lists
table。但是你可能已经删除了这个文件。
开发中没有问题,因为这个文件曾经存在并且执行了todo_lists
table创建。但是,如果您删除此文件并要求 Heroku 上的 Rails 应用引用它,它就不能再这样做了。
然后你得到一个错误。
永远不要删除任何迁移文件。因为您逐步执行更改(新的 tables,销毁 tables..),所以您应该保持一切不变。或者以后的部署(和数据库创建)无法再执行您要求他们执行的操作。
现在解决这个问题:如果您还有原始文件的副本,只需恢复它即可。否则只需删除(或编辑)任何提到 todo_lists
的迁移,无论是创建还是销毁,然后重置数据库并重新创建它。 (好像是个项目然后我估计没什么大不了的)
这次todo_lists
不会知道
我从我的迁移文件中删除了对 todo_list
或 todo_lists
的任何提及或引用,并提交了我的代码。
然后运行以下内容:
heroku run rails db:migrate