Tag: postgresql

为什么使用’gin’的add_index会创建一个’btree’索引呢?

我在PostgreSQL 9.3.4和Rails 4.0.4 。 我添加了一个“标签”列,以及相应的gin索引(或者,至少我要求一个)。 class AddTagsToPhotos < ActiveRecord::Migration def change add_column :photos, :tags, :text, array: true, null: false, default: [] add_index :photos, :tags, using: 'gin' end end 通过psqlvalidation结果: psql=# \d photos … tags | text[] | not null default ‘{}’::text[] Indexes: “index_photos_on_tags” btree (tags) 请注意,“标签”索引是btree类型 – 而我要求gin 。 现在手动创建一个索引以显示gin可用: psql=# create index index_photos_on_tags2 on […]

Rails 4 / postgresql index – 我应该使用一个datetime列作为索引filter,它可以有无限数量的值吗?

我需要优化查询以获取某个国家/地区的所有交易,然后才能在某个特定日期时间之前访问用户。 我的计划是实现以下索引 add_index(:deals,[:country_id,:last_user_access_datetime]) 我怀疑这个索引的相关性和有效性,因为列last_user_access_datetime可以具有日期的任何值ex:13/09/2015 3:06 pm并且它将经常更改(每次用户访问它时更新)。 如果我使用这个索引,这会使无限数量的值被索引? 我应该这样做还是避免使用’无限值的可能列,例如索引中的完全免费的datetime列?

无法部署到Heroku,因为服务器拒绝连接

我想将我的本地Rails应用程序(完美地运行)部署到Heroku,但是收到以下错误消息: rake aborted! could not connect to server: Connection refused Is the server running on host “127.0.0.1” and accepting TCP/IP connections on port 5432? /tmp/build_21pkcz898c28o/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:1216:in `initialize’ 其次是关于postgres_adapter的很多行。 我有点失望,因为我认为Heroku覆盖了config / database.yml,所以为什么它谈论在127.0.0.1上运行服务器(我不是在寻找远程数据库)? 谢谢, 更新 如果这有帮助,运行heroku config将提供以下内容: DATABASE_URL: xxx HEROKU_POSTGRESQL_GRAY_URL: xxx (the same xxx as above)

模型属性不会更新,在不更新的单独属性上调用validation错误?

我有以下半高级数据库查询,该查询过去10年的每小时价格并返回过去七天的每日平均价格: averages = Trade.where(‘date >= ?’, 7.days.ago).average(:price, :group => “DATE_TRUNC(‘day’, date – INTERVAL ‘1 hour’)”) 这将返回date (当天)和averageprice如下所示: “2012-12-29 00:00:00″=># 然后我遍历每个响应并将它们保存为TradeDailyAverage模型中的新记录。 # Loops through each daily average produced above averages.each do |date, avg| # Converts the BigDecimal to Floating Point(?) averagefloat = avg.to_f # Rounds the Daily Average to only two decimal points dailyaverage = number_with_precision(averagefloat, […]

在PostgreSql中有关联的子句

这是我的关联 Instructor has many Instructor Students InstructorStudent has many Student Contacts Student Contacts表有一个字段primary_contact,它有布尔类型。 我希望没有任何学生联系人或所有学生联系人的primary_contact的教师学生都是假的。 例如 教师学生1有3个学生联系人,所有3个学生联系人的主要联系人字段都是假的。 比我想要的记录 教师学生2有3个学生联系人,其中2个学生联系人的主要联系人字段是假的,而其他学生联系人的主要联系人是真实的,而不是我不想要该记录。 我做了一个查询,但我不怎么写有条款。 Instructor.joins(:instructor_students => :student_contacts) .where(“admin_users.id = ? AND student_contacts.primary_contact = ?”,7,false) .select(“student_contacts.instructor_student_id,count(student_contacts.primary_contact) as sc_count”) .group(“student_contacts.instructor_student_id”) .having(“count(student_contacts.primary_contact) = ?”,Dynamic_value) 任何帮助将不胜感激。 先感谢您。

奇怪的舍入问题

使用小数和浮点数发生了一些非常奇怪的事情,我无法理解为什么或在哪里(ruby / rails / postgresql)。 给定带有小数列的购买表 – 总计 : p1 = Purchase.where(total: 5.99).first_or_create p2 = Purchase.where(total: 5.99).first_or_create [p1.id, p2.id] # => [1, 2] p3 = Purchase.where(total: 5.99.to_d).first_or_create p4 = Purchase.where(total: 5.99.to_d).first_or_create [p3.id, p4.id] # => [1, 1] 无论小数或浮点数,Ruby和postgresql都没有完全代表5.99的问题: 5.99.to_s # => “5.99” 5.99.to_d.to_s # => “5.99” 5.99 == 5.99.to_d # => true SELECT CAST(5.99 AS […]

在Heroku上托管的ROR中,在2个站点上显示一个具有不同前端但相同后端的应用程序

我在rails中有一个应用程序,并在Heroku上托管。 现在,我想要的是可以从2个不同的域访问应用程序,当从假设a.com访问应用程序它应该有一个特定的前端,并且当从b.com访问时它应该显示一些其他前端。 两者的后端应该相同。 为了达到这个目的,我想我只有一种方法,就是让两个不同的应用程序使用相同的支持代码和相同的数据库(我正在使用PostgreSQL)。 我需要一些帮助,如何实现这一点,如果没有,那么其他一些替代方案。

ps auxwww | grep psql – postgres没有运行自制软件

我刚刚通过Homebrew安装了Postgres 9.2.3,启动了一个数据库,并输入了启动命令。 它说“服务器启动”,但每当我尝试做任何事情时,它都会告诉我: psql: could not connect to server: Connection refused Is the server running locally and accepting connections on Unix domain socket “/tmp/.s.PGSQL.5432”? 以下是我已经尝试解决此问题的方法: 1)通过添加以下文本修复了我的bash配置文件中的路径: export PATH=/usr/local/bin:$PATH 2)卸载并重新安装pg gem 3)使用了gem原始function 4)尝试卸载并重新安装家庭酿造 5)删除Mac附带的旧版postgres 6) sudo chmod -R 777 /var/pgsql_socket/ 尽管如此,我没有做任何事情。 我甚至尝试过使用Postgres官方安装程序和Heroku的Postgres应用程序。 什么都行不通。 似乎我无法让postgres正常运行,因为当我运行时: ps auxw | grep postgres ps auxw | grep postgres 我得到: [MyName] […]

如何从使用Heroku PG备份的备份中恢复三个项目?

我在Heroku上运行了一个Rails应用程序并安装了免费的pgbackups插件。 我想从备份中恢复三条记录。 根据Heroku docs进行还原时,它会还原整个数据库。 如何恢复这三个记录?

如何在Rails中基于外键列选择唯一记录?

我的Rails 4.1应用程序中有以下模型结构: delivery_service.rb class DeliveryService < ActiveRecord::Base attr_accessible :name, :description, :courier_name, :active, :country_ids has_many :prices, class_name: 'DeliveryServicePrice', dependent: :delete_all end delivery_service_price.rb class DeliveryServicePrice < ActiveRecord::Base attr_accessible :code, :price, :description, :min_weight, :max_weight, :min_length, :max_length, :min_thickness, :max_thickness, :active, :delivery_service_id belongs_to :delivery_service end 如您所见,送货服务有很多送货服务价格。 我正在尝试从送货服务价格表中检索记录; 选择外键唯一范围内具有最低价格属性的记录 ,delivery_service_id(基本上是每个交付服务的最便宜的交付服务价格)。 如何使用外键属性作为范围从表中选择唯一记录? 我希望我已经解释得足够了,如果您需要更多信息,请告诉我。 谢谢 更新#1: 我正在努力实现的例子: delivery_service_prices表: id:1,价格:2.20,delivery_service_id:1 id:2,价格:10.58,delivery_service_id:1 id:3,价格:4.88,delivery_service_id:2 id:4,价格:1.20,delivery_service_id:2 id:5,价格:14.99,delivery_service_id:3 […]