0

I have a query problem with sqlite and MariaDB. In development I use sqlite but in server we use MariaDB. In development following query works

Author.includes(:books, employes: :universities).where("universities.id is ? and books.year is ?",
                   @author.employes.first.universities.first.id, @book.jahr).references(:employes, :books)

but in production this gives error. When I change is to =, it works in server but does not produce any result in local.

Author.includes(:books, employes: :universities).where("universities.id = ? and books.year = ?",
                   @author.employes.first.universities.first.id, @book.jahr).references(:employes, :books)

This works in server but no result is produced in local.

Edit What if I have something like this?

Author.includes(:books, employes: :universities).where("universities.id = ? and books.year = ? and employes.name like ? and id IN ?",
                       @author.employes.first.universities.first.id, @book.jahr, "%#{@employe.name}%", [1,2,3]).references(:employes, :books)
1
  • 1
    First: Basic architecture design failure using different database servers on development and production ... Second check the generated SQL queries. Commented Aug 2, 2017 at 10:08

1 Answer 1

2

Firstly as Raymond said, never use different databases in development and production environment.

But for now you can avoid the SQL syntax errors by going ActiveRecord way, try this

Author.includes(:books, employes: :universities).where(
  universities: { id: @author.employes.first.universities.first.id },
  books: { year: @book.jahr }
).references(:employes, :books)

Since there's no ActiveRecord alternative for like, you can try this (it will work since like is valid in both MySQL and SQLite)

Author.includes(:books, employes: :universities).where(
  authors: { id: [1,2,3] },
  universities: { id: @author.employes.first.universities.first.id },
  books: { year: @book.jahr }
).where('employes.name like ?', "%#{@employe.name}%").references(:employes, :books)

Hope that helps!

Sign up to request clarification or add additional context in comments.

2 Comments

If pass where a Hash, it doesn't need to use references
Hi! it worked great. Could you show me what would be the ActiveRecord way of doing the query in the Edit in question above?

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.