Tag: postgresql

Rails 3迁移:boolean(mysql vs postgreSQL)

我正在尝试在论坛主题上添加“粘性”选项。 这就是我的迁移的样子 def self.up add_column :topics, :sticky, :boolean, :null => false, :default => false end def self.down remove_column :topics, :sticky end 这在mysql上本地完美,但是当我将更改推送到heroku(使用PostgreSQL)时,这就是我在使用控制台时所获得的 >> t.sticky => “f” >> t.sticky.class => String >> t.sticky = true => true >> t.sticky.class => TrueClass 为什么这个属性的默认值是String? 编辑:如果我保存对象,它不会改变粘性属性,即它仍然是“f”。

使用Rails 4 Activerecord将多个列计数合并到单个查询中

Rails 4.1,Postgres 9.3,部署到Heroku 我正在尝试减少对数据库的调用次数。 我有一个大表,调查,有多个布尔列,如role_composer , role_performer等。 控制器有多个查询,如 @sample = Survey.where(…whatever…) @Composers = @sample.count(“case when role_composer then true end”) … @Performers = @sample.count(“case when role_performer then true end”) 这样可以正常工作,但会导致对数据库的许多单独查询只有select中的表达式不同。 有没有办法将此构造为一个具有多个聚合/计算列的查询? 我也有使用average()和表达式的查询,但最常见的是count()。 在postgres中,这有效: SELECT count(case when role_composer then true end) as “COMPOSERS”, count(case when role_performer then true end) as “PERFORMERS” from surveys; 使用@sample上的Activerecord方法而不是求助于find_by_sql()的任何方法吗? 我尝试了各种方法但没有成功: .count().count() .select(“count(…) […]

“运行rspec时,无法在任何源中找到pg-0.12.2”

我正在使用Hartl的Rails教程构建twitter克隆应用程序。 我克隆了我的git repo( https://github.com/stewartmccoy/rails_tutorial )并将代码放到我以前在我的工作计算机上的个人计算机上。 因此,我的开发人员环境可能与我的Gemfile中指定的略有不同。 我的环境:Mac OS 10.6.8 Rails 3.2.13 ruby​​ 1.9.3p392(2013-02-22修订版39386)[x86_64-darwin10.8.0] *本地gem* 动作邮件(3.2.13,3.2.11,3.2.1) actionpack(3.2.13,3.2.11,3.2.1) activemodel(3.2.13,3.2.11,3.2.1) activerecord(3.2.13,3.2.11,3.2.1) activeresource(3.2.13,3.2.11,3.2.1) activesupport(3.2.13,3.2.11,3.2.1) 可寻址的(2.3.2) 注释(2.5.0) arel(3.0.2) bcrypt-ruby(3.0.1) bigdecimal(1.1.0) bootstrap-sass(2.1.0.0) bootstrap-will_paginate(0.0.6) 建设者(3.0.4) 捆绑器(1.3.4) 水豚(2.0.2,1.1.2) 子进程(0.3.9,0.3.6) 咖啡轨(3.2.2) 咖啡脚本(2.2.0) coffee-script-source(1.6.2,1.6.1,1.4.0) 黄瓜(1.2.1) 黄瓜栏杆(1.2.1) database_cleaner(0.7.0) 差异(1.2.1,1.1.3) erubis(2.7.0) execjs(1.4.0) factory_girl(4.1.0) factory_girl_rails(4.1.0) faker(1.0.1) ffi(1.4.0,1.3.1) 小黄瓜(2.11.5) 徒步旅行(1.2.1) i18n(0.6.1) io-console(0.3) 旅程(1.0.4) jquery-rails(2.2.1,2.1.4,2.0.2) json(1.7.7,1.7.6,1.7.5,1.5.5) libwebsocket(0.1.7.1) 邮件(2.5.3,2.4.4) 哑剧型(1.21,1.19) minitest(2.5.1) […]

Rails 4 Postgresql数组数据类型:更新值

我刚刚开始在Postgres中使用Rails 4中的数组数据类型,但是我无法在现有数组上获取值以进行更新。 我有一个名为’quantity’的列,它是一个整数数组([0,0])。 我想更新’quantity [0]’的值,我在控制台中尝试了以下内容: a = Asset.find(2) => # a.quantity[0] = 5 => 5 a.quantity_will_change! => [5, 0] a.save => true a.reload => # 如您所见,资产对象的数量值是变化的,但是当我尝试使用“a.save”保存对象时,重新加载对象时不会反映更改。 任何帮助将不胜感激。 谢谢

Rails Postgresfunction索引

我应该如何将包含函数的多语言索引输入schema.rb? 例如,这不起作用: add_index “temporary_events”, [“templateinfoid”, “campaign”, “date(gw_out_time)”, “messagetype”], :name => “temporary_events_campaign_tinfoid_date_messagetype” rake db:test:load 耙子流产了! PGError:错误:列“date(gw_out_time)”不存在 :CREATE INDEX“temporary_events_campaign_tinfoid_date_messagetype”ON“temporary_events”(“templateinfoid”,“campaign”,“date(gw_out_time”,“messagetype”)

Rails 4原始SQL请求中的字符串插值

在没有插值的情况下重写此查询的最佳方法是什么? def case_joins(type) subquery = <<-SQL.squish SELECT id FROM cases c2 WHERE c2.title_id = titles.id AND c2.value = 0 AND c2.type = '#{type}' ORDER BY c2.created_at DESC LIMIT 1 SQL "LEFT OUTER JOIN cases ON cases.title_id = titles.id AND cases.value = 0 AND cases.type = '#{type}' AND cases.id = (#{subquery})" end

如何在postgres中执行此组ActiveRecord查询

我试图通过3M表找到所有具有相同用户名的用户。 我读到这样的东西可能会成功。 User.find(:all, :group => [:username], :having => “count(*) > 1” ) 但是因为我正在使用Postgres,所以返回ActiveRecord::StatementInvalid: PG::Error: ERROR: column “users.id” must appear in the GROUP BY clause or be used in an aggregate function 。 我正在尝试这样的事情 User.select(‘users.id, users.username’).having(“count(*) > 1”).group(‘users.username’) 但仍然得到相同的错误。 知道我做错了什么吗? 更新:我以某种方式使用User.select(‘users.*’).group(‘users.id’).having(‘count(users.username) > 1’)但是这个查询返回给我看起来像即使创建了5条记录,也是一个空数组。 GroupAggregate (cost=9781143.40..9843673.60 rows=3126510 width=1365) Filter: (count(username) > 1) -> Sort (cost=9781143.40..9788959.68 rows=3126510 width=1365) […]

为什么我不能成功使用rake db:drop或rake db:purge?

有效的命令: rake db:migrate rake db:rollback rake db:seed 不能执行的命令: rake db:drop rake db:drop:all rake db:migrate:reset rake db:purge 如果我使用db:rollback足够多次(或使用STEP),我的数据库会成功删除,但不会在我使用db:drop / db:drop:all时删除。 db:migrate:reset和db:purge保持所有数据不变。 通过数据库监视器运行跟踪,观察development.log,系统日志和查看数据库查询(似乎没有SQL查询为这些命令运行。)也没有错误。 运行: Rails 4.2 postgres 9.4 CentOS 7

RoR Postgresql时区组不在Heroku上工作

我希望我的报告在CST中显示,而我的数据库是UTC。 为此,我需要能够按时间列进行分组并让db进行时区转换。 这适用于我的dev postgresql框: offset = -5 result = ActiveRecord::Base.connection.execute(“select date(srl.created_at AT TIME ZONE ‘#{offset.to_s}’) AS date, count(*) from server_request_logs srl where srl.created_at between ‘#{@from.to_s(:db)}’ and ‘#{@to.to_s(:db)}’ group by date(srl.created_at AT TIME ZONE ‘#{offset.to_s}’)”) 在heroku上它出错了: ActiveRecord::StatementInvalid: PGError: ERROR: time zone “-5” not recognized 它也没有使用像’America / Winnipeg’这样的TZInfo名称或者来自http://www.postgresql.org/docs/7.2/static/timezones.html的 ‘CST’支持的时区。 有没有人能够使这个工作?

Postgres选择一个concat字段并在Rails中ILIKE它

非常简单,试图这样做 SELECT (artist_name || ‘ ‘ || name) as full_name FROM “songs” WHERE “songs”.”working” = ‘t’ AND (full_name ILIKE(‘%Jack Beats%’)) AND (full_name ILIKE(‘%Epidemic%’)) AND (full_name ILIKE(‘%Dillon Francis%’)) ORDER BY songs.published_at asc LIMIT 1 但我明白了 ActiveRecord::StatementInvalid: PG::Error: ERROR: column “full_name” does not exist 我已经尝试在工作站之前添加表名,但没有效果。