Converting rails query from Mysql to PostgreSQL - mysql

Can anyone tell me how to convert this query to PostgreSQL
routes_controller.rb
#routes = Route.joins([:departure_location, :destination_location]).where("mdm_locations.name like ? or destination_locations_mdm_routes.name like ?" , "%#{k}%", "%#{k}%")
routes.rb (model)
module Mdm
class Route < ActiveRecord::Base
belongs_to :uom
belongs_to :distance_uom, :class_name => "Uom", :foreign_key => "distance_uom_id"
belongs_to :location
belongs_to :departure_location, :class_name => "Location", :foreign_key => "departure"
belongs_to :destination_location, :class_name => "Location", :foreign_key => "destination"
has_many :voyages, :dependent => :restrict
attr_accessible :description, :distance, :distance_uom_id, :departure, :std_consm, :destination, :uom_id
validates_presence_of :departure, :destination
end
end
Error :
PG::Error: ERROR: operator does not exist: integer = character varying
LINE 1: ...NNER JOIN "mdm_locations" ON "mdm_locations"."id" = "mdm_rou...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT COUNT(*) FROM "mdm_routes" INNER JOIN "mdm_locations" ON "mdm_locations"."id" = "mdm_routes"."departure" INNER JOIN "mdm_locations" "destination_locations_mdm_routes" ON "destination_locations_mdm_routes"."id" = "mdm_routes"."destination" WHERE (LOWER(mdm_locations.name) like '%futong%' or LOWER(destination_locations_mdm_routes.name) like '%futong%')

Your error message says:
operator does not exist: integer = character varying
and points you at this part of the SQL:
INNER JOIN "mdm_locations" ON "mdm_locations"."id" = "mdm_routes"."departure"
-- ------------------------------------------------^
Combining those tells us that mdnm_locations.id is an integer (as expected) but mdm_routes.departure is a varchar. You can't compare integers and strings in SQL without explicitly casting one of them to make the types compatible.
You need to fix your schema, mdm_routes.departure should be an integer column, not a string.
MySQL tries to be friendly by attempting to guess your intent and lets you get away with a lot of sloppy practices. PostgreSQL tries to be friendly by forcing you to say exactly what you mean to avoid confusions, incorrect guesses, and hidden bugs.

Related

Rails: how to join two models through two different relations?

I have two models: Saft (a magazine) and Keyword. Each "Saft" is defined by a series of keywords, but also has a title, which is always one of its keywords. The Saft and Keyword models are connected through a HABTM join table in order to pull all the keywords and now I am trying to pull the title from the keywords table onto the saft/show.html.erb, too. I am trying to use the class_name option in order to pull the title. Therefore I created the Edition model.
class Saft < ActiveRecord::Base
# attr_accessible :colour, :cover_alt, :description, :number, :short
has_and_belongs_to_many :keywords, :join_table => "safts_keywords"
has_one :title, :through => :edition, :class_name => "keyword"
has_one :edition
end
class Keyword < ActiveRecord::Base
# attr_accessible :word, :description
has_and_belongs_to_many :safts, :join_table => "safts_keywords"
belongs_to :issue, :through => :edition, :class_name => "saft"
belongs_to :edition
end
class Edition < ActiveRecord::Base
# attr_accessible :saft_id, :keyword_id
belongs_to :title
belongs_to :issue
end
class SaftsController < ApplicationController
def show
#saft = Saft.find(params[:id])
end
show.html.erb
<%= #saft.title.upcase %>
I get the following error:
Started GET "/safts/2" for 127.0.0.1 at Sat Feb 10 17:31:28 +0100 2018
Connecting to database specified by database.yml
Processing by SaftsController#show as HTML
Parameters: {"id"=>"2"}
Saft Load (1.8ms) SELECT `safts`.* FROM `safts` WHERE `safts`.`id` = ? LIMIT 1 [["id", "2"]]
Image Load (0.3ms) SELECT `images`.* FROM `images` WHERE `images`.`saft_id` = 2
Rendered safts/show.html.erb within layouts/public (35.0ms)
Completed 500 Internal Server Error in 103ms
ActionView::Template::Error (uninitialized constant Saft::keyword):
29: </div>
30: <div class="saft_box col-content">
31: <div class="saft_keyword">
32: <strong><%= #saft.title.upcase %></strong>
33: </div>
34: <div class="saft_description">
35: <p><%= #saft.description %></p>
app/views/safts/show.html.erb:32:in `_app_views_safts_show_html_erb___758994895_2167416580'
How can I get this working?
When uncommenting #saft.title.upcase this implementation also breaks the Saft Keyword association, which I get working again by uncommenting the belongs_to :issue section in the Keyword model.
Some changes in your models:
class Saft < ActiveRecord::Base
#You don't need attr_accessible for fields in safts table
has_and_belongs_to_many :keywords, :join_table => "safts_keywords"
#The specific keyword that acts as title.
#You need a new field in safts table named title_id which references to a Keyword.
belongs_to :title, class_name => "Keyword", :foreign_key => 'title_id'
end
class Keyword < ActiveRecord::Base
#You don't need attr_accessible for fields in keywords table
has_and_belongs_to_many :safts, :join_table => "safts_keywords"
end
To get the Saft title, you use #saft.title.word.upcase if #saft.title
I don't think you need anything else on your models for the use case in your OP.

Rails searches the mistaken has_and_belongs_to_many table

I want to show all types which are related to a specific organisation in a select box of my document form. Types are part of the Ar engine. Organisations are part of another existing engine.
module Ar
module OrganisationPatch
extend ActiveSupport::Concern
included do
attr_accessible :ar_document_id
has_many :ar_documents, :class_name => 'Ar::Document'
has_and_belongs_to_many :ar_types, :class_name => 'Ar::Type'
end
end
end
module Ar
class Type < ActiveRecord::Base
attr_accessible :name
has_many :documents
has_and_belongs_to_many :organisations
end
end
class CreateTypeOrganisations < ActiveRecord::Migration
def change
create_table :ar_type_organisations, id: false do |t|
t.uuid :type_id, index: true
t.uuid :organisation_id, index: true
end
end
end
In my documents_controller I load types for forms about the before filter. The superior returns the organisation object:
def load_form_objects
unless current_user.admin?
#types = current_user.superior.ar_types
else
#types = Type.all
end
end
Calling the page I get this error and ask me why he is looking for a table called organisations_types:
ActiveRecord::StatementInvalid in Ar/documents#new
Mysql2::Error: Table 'portal.organisations_types' doesn't exist:
SELECT ar_types.* FROM ar_types INNER JOIN organisations_types
ON ar_types.id = organisations_types.type_id WHERE
organisations_types.organisation_id =
x'891c3986b33845d08d3951645a4f27d5'
Someone knows what I am doing wrong here?
Your table name isn’t map with lexical order what has_and_belongs_to_many expect. ( Expected order is organisations_types )
So you have to add :join_table option in both model's association. Like this,
has_and_belongs_to_many :ar_types, :class_name => 'Ar::Type', join_table: "ar_type_organisations"
has_and_belongs_to_many :organisations, join_table: "ar_type_organisations"
Reference

Thinking sphinx index with Multiple joins and conditions

I have 3 models : Users has_one Service and Users Has_many Phones (verified or not, destroyed or not)
Class Phone < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_one :service
has_many :phones
has_many :verified_phones, -> {where('verified_at IS NOT NULL AND destroyed_at IS NULL')}, class_name: 'Phone'
end
class Service < ActiveRecord::Base
belongs_to :user
has_many :phones, through: :user
has_many :verified_phones, through: :user
end
I'd like to define an TS-index on the model Service.
I want an boolean-faceted attribute which represents Service whose user has one or more verified phone.
So after reading this post I tried :
join verified_phones
has 'COUNT(verified_phones.id) > 0', as: :user_phone_verified, type: :boolean, facet: true
but send me the error "Unknown column 'verified_phones.id' in 'field list' " (the same error occured in the post)
Then I tried
join verified_phones
has 'COUNT(phones.id) > 0', as: :user_phone_verified, type: :boolean, facet: true
-> but result are wrong : attribute is true for every user that have a phone, verified or not, destroyed or not.
Then I tried
join phones
has 'COUNT(phones.verified_at IS NOT NULL AND phones.destroyed_at IS NULL) > 0', as: :user_phone_verified, type: :boolean, facet: true
-> same problem : attribute is true for every user that have a phone, verified or not, destroyed or not.
I'm not good with SQL syntax, could anybody help me to solve this ?
Thanks
The second approach is what you're after - even though you're joining on the special association, the table alias will be the table name by default - hence, phones.id.
That said, what's being returned doesn't seem to be right. Perhaps the following instead:
join verified_phones
has "SUM(CASE WHEN phones.id IS NULL THEN 0 ELSE 1 END) > 0",
as: :user_phone_verified, type: :boolean, facet: true
I got it :
I should have been more precise with the syntax of my association definition :
has_many :verified_phones, -> {where('verified_at IS NOT NULL AND destroyed_at IS NULL')}, class_name: 'Phone'
should have been:
has_many :verified_phones, -> {where('phones.verified_at IS NOT NULL AND phones.destroyed_at IS NULL')}, class_name: 'Phone'
My User model has the same attribute "destroyed_at" -> the generated SQL was looking this attribute in the wrong table.
So with this correction both syntax work:
my initial :
join verified_phones
has 'COUNT(phones.id) > 0', as: :user_phone_verified, type: :boolean, facet: true
or the suggested by #Pat :
has "SUM(CASE WHEN phones.id IS NULL THEN 0 ELSE 1 END) > 0",
as: :user_phone_verified, type: :boolean, facet: true
But I'm not very confortable with this because I see possible confusion with this table alias still named "phones". What happen if I need to join in the same index phones and verified_phones?
But for now it solve my problem, so thank you #Pat !

Thinking Sphinx - How to index values in associated models which might be nil?

I am trying to define the following index on my Category model:
define_index do
has document.author.name :as => :author_name, :facet => true
end
My Model definitions are:
class Category < ActiveRecord::Base
has_many: documents
end
class Author ActiveRecord::Base
has_many :documents
end
class Document ActiveRecord::Base
belongs_to :category
belongs_to :author
end
A category may or may not have a document associated with it - depends on the category, many categories can exist without any documents.
The problem is when I try to run the indexer I get:
Cannot automatically map column type NilClass to an equivalent Sphinx
type (integer, float, boolean, datetime, string as ordinal). You could try to
explicitly convert the column's value in your define_index block:
has "CAST(column AS INT)", :type => :integer, :as => :column
Has anyone run into this issue?
define_index do
# firstly, you must have at least one indexed column
indexes document.author.name :as => :author_name, :facet => true
# to add 'has' for string you crc32
# has "CRC32(string_col)", :as => :filtered_string_col
end
if you need to search on that 'has' col:
:conditions => { "string to filter on".to_crc32 }

Tricky MySQL Query for messaging system in Rails - Please Help

I'm writing a facebook style messaging system for a Rails App and I'm having trouble selecting the Messages for the inbox (with will_paginate).
The messages are organized in threads, in the inbox the most recent message of a thread will appear with a link to it's thread. The thread is organized via a parent_id 1-n relationship with itself.
So far I'm using something like this:
class Message < ActiveRecord::Base
belongs_to :sender, :class_name => 'User', :foreign_key => "sender_id"
belongs_to :recipient, :class_name => 'User', :foreign_key => "recipient_id"
has_many :children, :class_name => "Message", :foreign_key => "parent_id"
belongs_to :thread, :class_name => "Message", :foreign_key => "parent_id"
end
class MessagesController < ApplicationController
def inbox
#messages = current_user.received_messages.paginate :page => params[:page], :per_page => 10, :order => "created_at DESC"
end
end
That gives me all the messages, but for one thread the thread itself and the most recent message will appear (and not only the most recent message). I can also not use the GROUP BY clause, because for the thread itself (the parent so to say) the parent_id = nil of course.
Anyone got an idea on how to solve this in an elegant way? I already thought about adding the parent_id to the parent itself and then group by parent_id, but I'm not sure if that works.
Thanks
My solution would be to get a list of threads (which I'm assuming could be obtained by messages with no parent id). Then on the Message model, add a method that will find the latest message in the thread and return it. You can then use that method to obtain the latest method in each thread and put in a link to the head of the thread easily.
(Pseudo-)code:
class Message < ActiveRecord::Base
belongs_to :sender, :class_name => 'User', :foreign_key => "sender_id"
belongs_to :recipient, :class_name => 'User', :foreign_key => "recipient_id"
has_many :children, :class_name => "Message", :foreign_key => "parent_id"
belongs_to :thread, :class_name => "Message", :foreign_key => "parent_id"
def get_last_message_in_thread()
last_message = self
children.each do |c|
message = c.get_last_message_in_thread()
last_message = message if message.created_at > last_message.created_at
end
return last_message
end
end
class MessagesController < ApplicationController
def inbox
#messages = current_user.received_messages.find_by_parent_id(Null).paginate :page => params[:page], :per_page => 10, :order => "created_at DESC"
end
end
You could probably do a lot better than having a recursive function to find the last message in the thread, but it's the simplest solution I can think of to demonstrate the idea. I'm also not sure I have the correct syntax for finding unset parent id's in the inbox function, which is why I marked the code as pseudo code :)
giving the parent itself as parent makes it very easy to create queries that operate on the whole thread, because you can group (or anything similar) by parent_id.
if you handle the parents differently, all your queries have to take care of this too
The only efficient way would be to have a Thread model and use GROUP BY as you mentioned - Anything else would require iteration over the messages.
read update in comments
I figured the only good solution is using a second model to store the most recent messages for every thread (because of performance issues when using GROUP BY with a subselect, see my comments). It won't take a lot of space in the DB because we're only storing id's and no text or even blobs.
The RecentMessages Model would look something like this:
create_table :recent_messages do |t|
t.integer :sender_id
t.integer :recipient_id
t.integer :message_id
t.integer :message_thread_id
t.timestamps
end
class RecentMessage < ActiveRecord::Base
belongs_to :message
belongs_to :message_thread, :class_name => 'Message'
belongs_to :sender, :class_name => 'User', :foreign_key => "sender_id"
belongs_to :recipient, :class_name => 'User', :foreign_key => "recipient_id"
end
The main idea is: All the messages are stored in one model (Messages). Whenever a new message is added to a thread (or a thread is created), two things happen (e.g. with a after_save callback):
Store the new message in the RecentMessages model (that means sender_id, recipient_id, message_id, message_thread_id (= parent_id || id))
Get the most recent message (from this thread in messages), where sender_id == recipient_id and vice versa (note: This only works if the message model should only support messages between 2 users) and store it in the RecentMessages model as well (if found and if it's not already there)
Of course there should only be max. 2 recent_messages stored in the DB for every message_thread at any given time.
If one wants to show i.e. the inbox, the following has to happen:
#messages = current_user.recent_received_messages.paginate :page => params[:page], :per_page => 10, :order => "created_at DESC", :include => :message
That's the best I figured out so far. I still think it's ugly but it's fast and it works. If anyone comes up with a better solution, I'll be gratefull!
I don't know how to accomplish this in Rails, but this is how I did it directly in MySQL:
select * from messages where message_id in (
select max(message_id) from messages where to_uid = 51 group by thread_id
) order by timestamp desc
I used a subquery to grab the most recent message in a thread and then the main query to grab all of the fields for the messages found in the subquery.