Tag: postgresql

新分配的序列不起作用

在PostgreSQL中,我创建了一个新表并为id列分配了一个新序列。 如果我从PostgreSQL控制台插入一条记录它可以工作,但是当我尝试从Rails导入一个记录时,它会引发一个exception,它无法找到相关的序列。 这是表格: \d+ user_messages; Table “public.user_messages” Column | Type | Modifiers | Storage | Description ————-+—————————–+————————————————————+———-+————- id | integer | not null default nextval(‘new_user_messages_id’::regclass) | plain | 但是当我尝试使用Rails使用的SQL查询获取序列时,它返回NULL: select pg_catalog.pg_get_serial_sequence(‘user_messages’, ‘id’); pg_get_serial_sequence ———————— (1 row) Rails引发的错误是: UserMessage.import [UserMessage.new] NoMethodError: undefined method `split’ for nil:NilClass from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:910:in `default_sequence_name’ 仅当我使用ActiveRecord扩展名导入批量记录时,才会出现此问题,单个记录通过ActiveRecord保存。 我如何解决它?

ORDER BY使用Active Record&Rails有时为空的列

在我的rails应用程序中(使用postgresql),我正在尝试编写一个Active Record查询来查找一组志愿者记录,然后通过first_name , last_name ,然后email进行last_name 。 此外, first_name和last_name可能为null (两者都将为null或两者都不为null )。 例如,我想跟踪列表进行如此排序: 志愿者[first_name:’Alex’,last_name:’Diego’,电子邮件:’a.diego@person.com’] 志愿者[first_name: null ,last_name: null ,电子邮件:’cxxr@person.com’] 志愿者[first_name:’Josh’,last_name:’Broger’,电子邮件:’broger@person.com’] 志愿者[first_name:’Josh’,last_name:’Broger’,电子邮件:’jcool@person.com’] 志愿者[first_name:’Josh’,last_name:’Kenton’,电子邮件:’aj@person.com’] 最初,我有以下代码: Volunteer.joins(:volunteer_lists). where(“(volunteer_lists.organizer_id = ? AND organizer_type = ‘Organization’) OR (volunteer_lists.organizer_id IN (?) AND organizer_type = ‘Collaborative’)”, self.organization.id, collaboratives).uniq. order(:first_name, :last_name, :email) 此代码有效,除了结果由志愿者首先使用first_name和last_name进行分组,其他志愿者仅使用最后email (因此在上面的示例列表中,志愿者#2将是最后一个)。 这个有用的post的答案表明我应该在语句的ORDER BY部分使用COALESCE()函数来获得我想要的结果。 真棒! 所以我将我的代码更新为以下内容: Volunteer.joins(:volunteer_lists). where(“(volunteer_lists.organizer_id = ? AND organizer_type = […]

用于rails应用程序的PG :: SyntaxError

在我的索引控制器中,我有一个查询来查找所有过去发生的事件“何时是事件发生的日期 @expired = Location.where(‘when <= ?', Date.today) 这是我的index.html.erb视图。 to General Smithridge Healthcare 当我运行服务器时,我得到以下错误: PG::SyntaxError: ERROR: syntax error at or near “when” LINE 1: SELECT “locations”.* FROM “locations” WHERE (when <= '2015-0… 可能是由于错误的格式化日期?

为什么Postgresql会像Geokit这样失败?

我刚刚开始使用Rails为我正在构建的应用程序玩geokit。 我的记录得到了地理编码,但是当我进入控制台看看我能用它做什么时它会爆炸以下:用户是一个用户对象,我的数据库中有lat和lng >> Restaurant.find(:all, :origin => user) Restaurant Load (0.0ms) PGError: ERROR: operator does not exist: numeric – character varying LINE 1: …*, SQRT(POW(111.1819*(-33.872517-restauran… ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. : SELECT *, SQRT(POW(111.1819*(-33.872517-restaurants.lat),2)+ POW(76.7136337302943*(151.205536-restaurants.lng),2)) AS distance FROM “restaurants” ActiveRecord::StatementInvalid: PGError: ERROR: […]

使用postgres在带轨道的DISTINCT ID中订购

我有以下代码用micropost_id列连接两个表微博和活动,然后根据具有不同微博ID的activities_at活动表进行排序。 Micropost.joins(“INNER JOIN activities ON (activities.micropost_id = microposts.id)”). where(‘activities.user_id= ?’,id).order(‘activities.created_at DESC’). select(“DISTINCT (microposts.id), *”) 它应该返回整个微柱。这在我的开发环境中不起作用。 (PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list 如果我在SELECT DISTINCT中添加activities.created_at,我会得到重复的微博ID,因为它有不同的activities.created_at列。 我已经做了很多搜索到达这里。 但是由于这种postgres条件,问题总是存在,以避免随机选择。 我想根据activities.created_at的顺序选择不同的micropost _id。 请帮忙..

在多服务器环境中,如果站点的不活动超过15分钟,则服务器将失去与PostgreSQL数据库的连接

如果我的暂存(2台服务器)或生产(4台服务器)服务器在大约15分钟内没有活动,我在airbrake中会出现以下错误。 以下是错误消息: ActiveRecord :: StatementInvalid:PG ::错误:无法从服务器接收数据:连接超时 要么 PG ::错误:无法连接到服务器:连接超时服务器是否在主机“tci-db4.dev.prod”上运行并接受端口5432上的TCP / IP连接? 我正在使用PostgreSQL作为我的数据库。 其中一个服务器也充当数据库服务器。 环境: Ruby 1.9.3 (这也发生在Ruby 1.8.7下,但是自升级以来更糟,因为服务器上的ruby进程将达到100%并保持100%直到服务器丢失数据库连接时被杀死。 Rails 3.1.6 PG GEM 0.13.2 Postgres 9.1 Phusion Passenger 这个问题已经发生了一年多,所以我希望有人对如何解决这个问题有一些了解。 谢谢。

Rails范围在月内创建

我在编写模型范围时遇到了一些问题。 我想基于它们发布的月份来过滤我的模型对象,即模型BlogPost: scope :published_in_month, ->(date) { where(published_date: date.at_beginning_of_month..date.at_end_of_month) } 所以这是我的控制器方法: def list_by_month @date = Time.parse(“#{params[:year]}/#{params[:month]}”) puts “DATE IS #{@date}” @posts = BlogPost.published_in_month(@date).page(params[:page]).per(10) render :index end 所以打印出来的日期我看到的是: DATE IS 2013-12-01 00:00:00 +0100 但是在我的日志和我看到错误月份的post的页面中,这是一个条目日志: SELECT “blog_posts”.* FROM “blog_posts” WHERE (“blog_posts”.”published_date” BETWEEN ‘2013-11-30 23:00:00.000000’ AND ‘2013-12-31 22:59:59.999999’) LIMIT 10 OFFSET 0 这个2013-11-30来自于我的输入日期是2013-12-01 ,如果我错误地用它来产生不正确的查询,我怎么能重写我的范围

rails postgres insert

我正在尝试按照Jonaphin给出的示例从csv文件导入数据,url为http://www.genesx.com/2012/06/import-csv-files-100x-faster-in-rails-3/ 我试图从postgres获取最后一个插入的索引,但由于某种原因代码不起作用。 def process_data!(file, survey_id) headers = [ “zip_code”, “booking_date” ] ActiveRecord::Base.establish_connection created_at = Time.now.strftime(“%Y-%m-%d %H:%M:%S”) CSV.foreach(file, {headers: :first_row}) do |row| sql_keys = [] sql_vals = [] headers.each_with_index do |key, idx| val = row[idx] sql_keys << key sql_vals << ActiveRecord::Base.connection.quote(val) end sql = " INSERT INTO bookings (survey_id, #{sql_keys.join(', ')}, created_at, updated_at) VALUES (#{survey_id}, #{sql_vals.join(', […]

DISTINCT ON查询w / ORDER BY列的最大值

我的任务是将一个Rails应用程序从MySQL转换为Postgres asap并遇到一个小问题。 活动记录查询: current_user.profile_visits.limit(6).order(“created_at DESC”).where(“created_at > ? AND visitor_id ?”, 2.months.ago, current_user.id).distinct 生成SQL: SELECT visitor_id, MAX(created_at) as created_at, distinct on (visitor_id) * FROM “profile_visits” WHERE “profile_visits”.”social_user_id” = 21 AND (created_at > ‘2015-02-01 17:17:01.826897’ AND visitor_id 21) ORDER BY created_at DESC, id DESC LIMIT 6 我在使用MySQL时非常自信,但我老实说是Postgres的新手。 我认为这个查询失败的原因有很多。 我认为需要先区分。 我不知道如何通过max函数的结果来订购 我甚至可以使用这样的max函数吗? 此查询的高级目标是返回用户的6个最新配置文件视图。 任何关于如何修复此ActiveRecord查询(或它的结果SQL)的指针将不胜感激。

如何在Postgres的Rails中按天分组和计数?

这个问题我如何按天而不是按日期分组? 展示了如何在Ruby中进行分组。 这可行,但对于大量记录来说会非常慢。 如何做到这一点,以便按日期分组和计算:created_at发生在Postgres内?