Rails 3.2.6和通过迁移创建数据库视图

时间:2023-01-15 23:14:42

I'm using rails 3.2.6 and I need to create a database VIEW. As usual I created a migration and I tried to achieve the goal using the execute method.

我使用的是rails 3.2.6,我需要创建一个数据库视图。像往常一样,我创建了一个迁移,并尝试使用execute方法实现目标。

Unfortunately the migration generates a table, not a view. Why?

不幸的是,迁移生成了一个表,而不是一个视图。为什么?

Many thanks in advance, Mauro

非常感谢你,毛罗

UPDATE:

更新:

I would like to have something as follows:

我想要以下内容:

class CreateMyView < ActiveRecord::Migration
  def self.up
    execute <<-SQL
      CREATE VIEW my_view AS SELECT ...
    SQL
  end
  def self.down
    execute <<-SQL
      DROP VIEW my_view
    SQL
  end
end

Unfortunately this migration creates a table...

不幸的是,这种迁移创建了一个表……

UPDATE: the previous code works! I was executing rake db:reset instead of rake db:migrate:reset (my mistake)

更新:以前的代码有效!我正在执行rake db:reset而不是rake db: migration:reset(我的错误)

2 个解决方案

#1


2  

It looks like you've answered your own question, but I'll make a related suggestion. Try the rails_sql_views gem. That link goes to the original repo on GitHub. It looks like it's not being maintained anymore, though. It would be worth looking at the network graph and trying one of the forks. I'm not positive that any of the forks supports Rails 3.2.6, but I'd suggest looking through them. Christian Eichhorn added support for the mysql2 adapter about three years ago.

看起来你已经回答了你自己的问题,但我将提出一个相关的建议。试rails_sql_views宝石。该链接指向GitHub上的原始repo。看起来它不再被维护了。看看网络图和尝试其中的一个分支是值得的。我不确定任何福克斯都支持Rails 3.2.6,但我建议仔细研究一下。大约三年前,Christian Eichhorn添加了对mysql2适配器的支持。

#2


2  

I've made a gem called rails_db_views which is compatible with Rails 4, and still maintained.

我已经创建了一个名为rails_db_views的gem,它与Rails 4兼容,并且仍然得到维护。

Regards,

问候,

Yacine.

Yacine。

#1


2  

It looks like you've answered your own question, but I'll make a related suggestion. Try the rails_sql_views gem. That link goes to the original repo on GitHub. It looks like it's not being maintained anymore, though. It would be worth looking at the network graph and trying one of the forks. I'm not positive that any of the forks supports Rails 3.2.6, but I'd suggest looking through them. Christian Eichhorn added support for the mysql2 adapter about three years ago.

看起来你已经回答了你自己的问题,但我将提出一个相关的建议。试rails_sql_views宝石。该链接指向GitHub上的原始repo。看起来它不再被维护了。看看网络图和尝试其中的一个分支是值得的。我不确定任何福克斯都支持Rails 3.2.6,但我建议仔细研究一下。大约三年前,Christian Eichhorn添加了对mysql2适配器的支持。

#2


2  

I've made a gem called rails_db_views which is compatible with Rails 4, and still maintained.

我已经创建了一个名为rails_db_views的gem,它与Rails 4兼容,并且仍然得到维护。

Regards,

问候,

Yacine.

Yacine。