Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何编辑迁移表_Ruby On Rails_Database_Ruby On Rails 4_Rake_Migrate - Fatal编程技术网

Ruby on rails 如何编辑迁移表

Ruby on rails 如何编辑迁移表,ruby-on-rails,database,ruby-on-rails-4,rake,migrate,Ruby On Rails,Database,Ruby On Rails 4,Rake,Migrate,我创建了我的模型,然后在我的迁移文件db/migration/234802843208\u create\uuxxxxx\u rb中填写了以下内容: class CreateXXXXX < ActiveRecord::Migration def change create_table :XXXXX do |t| t.string :gender t.date :date_of_birth t.string

我创建了我的模型,然后在我的迁移文件
db/migration/234802843208\u create\uuxxxxx\u rb
中填写了以下内容:

class CreateXXXXX < ActiveRecord::Migration
  def change
    create_table :XXXXX do |t|

                t.string  :gender
        t.date    :date_of_birth
        t.string  :Marital_status
        t.string  :Spouses_name
class CreateXXXXX
我运行了rakedb:migrate


现在我想编辑这个表。我想将状态更改为布尔数据类型,而不是字符串。有没有合适的方法?我可以进入一个编辑表并重新运行rake db:migrate吗?您建议我回滚、编辑,然后重新迁移,还是有更好的方法?

在这些情况下,我觉得最方便的方法是编辑文件,然后运行

rake db:migrate:redo
如果不可能,我建议回滚,然后在更新迁移类后进行迁移。

您可以这样做

rake db:rollback

然后打开迁移文件并更新更改

class CreateXXXXX < ActiveRecord::Migration
  def change
    create_table :XXXXX do |t|

        t.string  :gender
        t.date    :date_of_birth
        t.boolean  :Marital_status
        t.string  :Spouses_name

  end
class CreateXXXXX

然后执行
rake db:migrate
rake db:migrate:up

您应该创建另一个迁移,并编辑该字段

rails g migration edit_xxxx_marital_status
这将创建一个迁移文件。并向其中添加以下内容

class EditXXXXMaritalStatus < ActiveRecord::Migration
 def up
    change_column XXXXX. :Marital_status, :boolean, default: false # if you waNT TO add any default value
 end

 def down
   change_column :XXXXX, :Marital_status, :string 
 end
end
class editxxmaritalstatus
这似乎比其他答案更复杂。这种方法有什么优点?如果您必须在生产中执行相同的操作,并且表中有实时数据,那么通过执行回滚,您将丢失数据。
class EditXXXXMaritalStatus < ActiveRecord::Migration
 def up
    change_column XXXXX. :Marital_status, :boolean, default: false # if you waNT TO add any default value
 end

 def down
   change_column :XXXXX, :Marital_status, :string 
 end
end