0

While executing a simple select - where operation using activerecord execute,

ActiveRecord::Base.connection.execute('select * from spree_variants where sku = "1SB-E4196-00";')

I got this error:

from /Users/abc/.rvm/gems/ruby-2.7.2@cboparts/gems/activerecord-6.0.3.5/lib/active_record/connection_adapters/postgresql/database_statements.rb:92:in `exec'
Caused by PG::UndefinedColumn: ERROR:  column "1SB-E4196-00" does not exist
LINE 1: select * from spree_variants where sku = "1SB-E4196-00";

Why it is considering "1SB-E4196-00" as a column but not SKU? The error seems misleading.

1 Answer 1

4

Because PostgreSQL expects strings to be bounded in single quotes. While double quotes have a different meaning:

There is a second kind of identifier: the delimited identifier or quoted identifier. It is formed by enclosing an arbitrary sequence of characters in double-quotes ("). A delimited identifier is always an identifier, never a key word.

That means if the following query should work:

ActiveRecord::Base.connection.execute(
  "select * from spree_variants where sku = '1SB-E4196-00';"
)

Btw you if you are using Rails and have a SpreeVariant model then you can see in the console how Rails formats and escapes the query like this:

puts SpreeVariant.where(sku: '1SB-E4196-00').to_sql
Sign up to request clarification or add additional context in comments.

Comments

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.