7

我有一个名为my_view我使用以下迁移创建的视图。

class CreateMyView < ActiveRecord::Migration
  def change
    execute <<-SQL
      drop view if exists my_view
    SQL

    execute <<-SQL
      CREATE OR REPLACE VIEW my_view AS 
      SELECT 
        t1.wfs_id,
        t1.step_id,
        t1.status,
        t1.applied_by,
        t2.created_at,
        t2.is_wfs_end,
        t2.app_status AS flowstep
       FROM table1 t1
         JOIN table2 t2 ON t1.wfs_id = t2.wfs_id
      WHERE t1.del_flag = false;
    SQL
  end
end

现在我需要另一个字段,比如说my_new_fieldfromtable1可以在my_view. 但我不知道如何为此编写迁移。非常感谢任何帮助。谢谢

4

2 回答 2

9

简单地重新创建视图怎么样:

class ChangeMyView < ActiveRecord::Migration   
  def change
    execute <<-SQL
      drop view if exists my_view
    SQL

    execute <<-SQL
      CREATE OR REPLACE VIEW my_view AS 
      SELECT 
        t1.wfs_id,
        t1.step_id,
        t1.status,
        t1.applied_by,
        t1.my_new_field,
        t2.created_at,
        t2.is_wfs_end,
        t2.app_status AS flowstep
      FROM table1 t1
        JOIN table2 t2 ON t1.wfs_id = t2.wfs_id
      WHERE t1.del_flag = false;
   SQL 
  end

end
于 2016-11-15T11:51:19.583 回答
2

风景优美的宝石(由 Thoughtbot 提供)将帮助您管理您的视图!

于 2018-08-09T17:43:30.733 回答