【问题标题】:no migration via terminal没有通过终端迁移
【发布时间】:2018-01-16 06:45:47
【问题描述】:

我无法通过终端和 RoR 迁移数据库中的新列。 我只迈出了第一步:

**$ rails g migration add_online_to_posts online:boolean**

Running via Spring preloader in process 13406
      invoke  active_record
      create    db/migrate/20170808141302_add_online_to_posts.rb

之后没有任何效果:

**$ rails db:migrate**
rails aborted!
ActiveRecord::DuplicateMigrationVersionError: 

Multiple migrations have the version number 20170725122210.

/home/projets/RoR_formation/vendor/bundle/gems/activerecord-5.1.2/lib/active_record/migration.rb:1257:in `validate'
/home/projets/RoR_formation/vendor/bundle/gems/activerecord-5.1.2/lib/active_record/migration.rb:1123:in `initialize'
/home/projets/RoR_formation/vendor/bundle/gems/activerecord-5.1.2/lib/active_record/migration.rb:1007:in `new'
/home/projets/RoR_formation/vendor/bundle/gems/activerecord-5.1.2/lib/active_record/migration.rb:1007:in `up'
/home/projets/RoR_formation/vendor/bundle/gems/activerecord-5.1.2/lib/active_record/migration.rb:985:in `migrate'
/home/projets/RoR_formation/vendor/bundle/gems/activerecord-5.1.2/lib/active_record/tasks/database_tasks.rb:171:in `migrate'
/home/projets/RoR_formation/vendor/bundle/gems/activerecord-5.1.2/lib/active_record/railties/databases.rake:58:in `block (2 levels) in <top (required)>'
/home/projets/RoR_formation/vendor/bundle/gems/railties-5.1.2/lib/rails/commands/rake/rake_command.rb:21:in `block in perform'
/home/projets/RoR_formation/vendor/bundle/gems/railties-5.1.2/lib/rails/commands/rake/rake_command.rb:18:in `perform'
/home/projets/RoR_formation/vendor/bundle/gems/railties-5.1.2/lib/rails/command.rb:46:in `invoke'
/home/projets/RoR_formation/vendor/bundle/gems/railties-5.1.2/lib/rails/commands.rb:16:in `<top (required)>'
/home/projets/RoR_formation/bin/rails:9:in `require'
/home/projets/RoR_formation/bin/rails:9:in `<top (required)>'
/home/projets/RoR_formation/vendor/bundle/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
/home/projets/RoR_formation/vendor/bundle/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
/home/projets/RoR_formation/vendor/bundle/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
/home/projets/RoR_formation/vendor/bundle/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
/home/projets/RoR_formation/vendor/bundle/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
/home/projets/RoR_formation/vendor/bundle/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
/home/projets/RoR_formation/vendor/bundle/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
/home/projets/RoR_formation/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate

我可以通过 IDE 处理列:

但是当我这样做时:

**$ Status   Migration ID    Migration Name**
  up     20170725122210  Create posts table
  up     20170726082500  Rename post title to intitule
  up     20170728143925  Create categories
  up     20170807110617  ********** NO FILE **********
 down    20170808113533  Add slug to posts

【问题讨论】:

  • 您是如何创建迁移文件的?您是否使用了生成器,或者您是否复制和编辑了迁移文件?迁移文件必须是唯一的版本。
  • 确实,我通过终端使用了生成器。但是对于教程的步骤,我手动删除了文件

标签: ruby-on-rails database-migration


【解决方案1】:

您已经使用前缀 20170725122210 运行了迁移(看起来您也可能已将其删除)。要快速修复,请将最新迁移更改为 20170725122211(或任何其他不在您的架构迁移表中的数字)。您可能还想运行 rails db:schema:dump 并检查 db/schema.rb 的输出以查找迁移中不存在的表。

【讨论】:

  • 确实,我通过终端使用了生成器。但是对于本教程的步骤,我手动删除了文件。当我运行 ** rails db:schema:dump ** 它什么也没做。 .因此我无法检查 db/schema.rb 的输出。我将我最新的迁移重构为 20170725122211 我还通知 Rubymine 允许从 IDE 运行和创建 db 迁移。我可以使用这个 ?option 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-19
  • 2011-06-01
  • 2017-06-24
  • 1970-01-01
  • 2018-10-08
相关资源
最近更新 更多