1

So here are the relationships:

class Tutor
  has_many :subjects, through: :profile
  has_one :profile, dependent: :destroy
end

class Profile
  belongs_to :tutor
  has_many :subjects, through: :profile_subjects
  has_many :profile_subjects
end

class Subject
  has_many :profile_subjects
  has_many :profiles, through: :profile_subjects
  has_many :tutors, through: :profiles
end

# the join model for Profile and Subject
class ProfileSubject
  belongs_to :profile
  belongs_to :subject
end

Now previously it was all working perfectly fine when I was using the sqlite3 database. But upon deployment to production, I'm getting thrown errors because the database is Postgres.

Here's a simple example of what I do that throws the error

subject = Subject.first
subject.tutors

And the error that is being returned is

ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR:  operator does not exist: integer = character varying

As I understand (or was told) it means I'm trying to compare (or join) a varchar to an integer field.

The bigger picture of what I'm actually trying to do is something like Subject.where("name LIKE ?", "#{name}") and be able to find all tutors that have the queried subject. How am I able to access tutors through subject?

(I would just like to note that the associations were working perfectly fine on sqlite3)

10
  • Didn't you ask the same question yesterday? The awnser yesterday was wrong column type. Both columns have to be of type integer. Commented Oct 27, 2016 at 7:05
  • Possible duplicate of Function draws Error with Postgres Databse but not on Sqlite Commented Oct 27, 2016 at 7:06
  • I did ask a similar question but i did not realise the root cause of the problem. Now i realised that i cannot even do something basic like stated subject = Subject.first followed by subject.tutors and why is that so? Which are the 2 columns that needs to be integers when i execute that command? Commented Oct 27, 2016 at 7:07
  • I am simply breaking down the problem and i would like to understand the root cause of the issue. Since i couldn't find anything with regards to it. Also i'm sure that if its a problem with columns it may help others who are working transitioning from sqlite3 to postgres too? Commented Oct 27, 2016 at 7:09
  • I can't extract it from your error message. Take a look at your migrations or schema.rb or take a look at the table definitions within postgres. All foreign keys should have the same datatype as the corresponding primary keys. A good choice is often integer. It's not a general sqlite problem. sqlite is only a little bit more gentle. You column types have to be right. Commented Oct 27, 2016 at 7:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.