The Sequel gem qualified query column name with table double underscores
Asked Answered
I

2

7

Using the Sequel gem:

employees = DB[:prm_master__employee.identifier]
.join(:prm_master__employee_custom_fields.identifier, :employee => :employee)
.where("termination_date >= ?","06/01/2012")
.or("termination_date = NULL")
.and("employee = 'holderl'")

The above fails with:

~/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/sequel-3.41.0/lib/sequel/adapters/tinytds.rb:221:in `fields': TinyTds::Error: Ambiguous column name 'employee'. (Sequel::DatabaseError)

I understand the error (same column names between joined tables, e.g employee), but do not know how I can qualify the employee condition in the and statement since the table uses the identifier method to ignore the underscores.

Isopiestic answered 3/12, 2012 at 8:11 Comment(2)
FYI: You're using three backticks as an attempt to mark a block of code. That doesn't work right. Use four spaces to indent instead. See the Advanced Markdown documentation for more information.Iridic
Github markdown is far superior. the four space indent is impossible.Isopiestic
I
15

The answer is to actually qualify the column name using:

Sequel.qualify(:table, :column)

Or the equivalent shorter syntax:

Sequel[:table][:column]

resulting in:

employees = DB[:prm_master__employee.identifier]
.join(:prm_master__employee_custom_fields.identifier, :employee => :employee)
.where("termination_date >= ?","06/01/2012")
.or("termination_date = NULL")
.and(Sequel.qualify(:prm_master__employee_custom_fields.identifier, :employee)  => "holderl")
Isopiestic answered 3/12, 2012 at 8:52 Comment(2)
This can also be used on model relationships to avoid ambiguity in JOINs.Courser
There's also the shorter syntax Sequel[:table][:column] (added it to the answer)Seedtime
N
0

User alias. OR put table_name.column_name

Naashom answered 3/12, 2012 at 8:23 Comment(1)
yes i understand that, but how to do that in sequel gem when double underscore cannot be used? see my answer.Isopiestic

© 2022 - 2024 — McMap. All rights reserved.