简体   繁体   English

无法在Rails上运行数据库迁移

[英]Can't run a db migration on rails

I have had ongoing trouble migrating a database in rails via rake db:migrate. 我一直无法通过rake db:migrate在rails中迁移数据库。

My migration currently looks like this: 我的迁移目前看起来像这样:

class CreateArticles < ActiveRecord::Migration
  def change
    create_table :articles do |t|
    t.string :title
    t.text :subtitle
    t.string :slug
    t.text :body
    t.integer :publish, limit: 1, default: 0
    t.timestamps
    end
  end
end

However if ever I delete a column from this or even add or modify one the rake db:migrate command does nothing. 但是,如果我从中删除一列,甚至添加或修改其中一列,rake db:migrate命令都不会执行任何操作。 The only way I can migrate it is sometimes running something like: 我迁移的唯一方法有时是运行类似的命令:

rake db:migrate VERSION=20080906120000

But even that is temperamental so most of the time I need to reset the db using 但是即使那样也很气质,所以大多数时候我需要使用

db:drop
db:create

then running the migration again as normal. 然后再次正常运行迁移。 basically db:migrate only ever works the first time after dropping and creating the db. 基本上db:migrate只能在删除和创建db后的第一次使用。

I've also tried rollback before running the migration. 在运行迁移之前,我还尝试了回滚。

This is far from ideal so I'd appreciate any help. 这远非理想,因此不胜感激。

(I realise there are similar questions but all of their issues are solved after a db reset) (我知道也有类似的问题,但是所有的问题都可以在重置数据库后解决)

You need to run the migration down and then run it up again, which is done with the redo task. 您需要运行迁移down ,然后运行它up一遍,这与做redo任务。 try this: 尝试这个:

rake db:migrate:redo VERSION=20080906120000

EDIT: if you have data you want to keep in this table, it's better to make a new migration to remove the column or whatever you want to do, as the above will drop and recreate the table, wiping the data in the process. 编辑:如果您有要保留在该表中的数据,最好进行新的迁移以删除该列或您想要执行的任何操作,因为上面的操作将删除并重新创建表,在此过程中擦除数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM