Search code examples
rubysequel

The Sequel gem qualified query column name with table double underscores


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.


Solution

  • 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")