使用SQL序列的迁移不适用于测试环境

戈马拉尔

我目前正在迁移

class CreateSlugSequence < ActiveRecord::Migration[5.2]
  def up
    execute <<-SQL
      CREATE SEQUENCE slug_sequence
    SQL
  end

  def down
    execute <<-SQL
      DROP SEQUENCE slug_sequence
    SQL
  end
end

我运行rails db:migrate

进入rails c(开发模式)

运行ActiveRecord :: Base.connection.exec_query(“ select nextval('slug_sequence')”)[0] ['nextval']

并获得期望值

但是,如果出于某种原因在测试模式下输入rails c,则序列表不存在

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "slug_sequence" does not exist

我运行ActiveRecord::Migrator.current_version,以检查上一次应用了什么迁移,并返回了最新版本。

我在这里先向您的帮助表示感谢。

奥斯蒂奥

我建议删除测试数据库并重新创建它。尽管这些应该保持同步,但有时却不同步,您必须执行手动步骤才能将它们组合在一起。

RAILS_ENV=test rake db:reset

奇怪的是,它不起作用,在该任务中放一个撬调试器并查看发生了什么会很有趣。https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake#L122

手动步骤

RAILS_ENV=test rake db:drop
RAILS_ENV=test rake db:create
RAILS_ENV=test rake db:migrate

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章