On Tuesday, November 27, 2018 at 6:55:35 AM UTC-8, Sachu Thomas Isaac wrote:
>
> Hi,
>
> Following are the 3 classes in which one class is of class table 
> inheritance
>
> class User < Sequel::Model
>   #table_name :users
>   plugin :class_table_inheritance, key: :type
> end
>
> class Employee < User
>   #table_name :employees
>   many_to_one :department
> end
>
> class Department < Sequel::Model
>   one_to_many :employees
> end
>
>
> @department.remove_employee(@employee) fails as it throws *an exception 
> stating that :id is ambiguous*
>
> The exact line which causes this problem is the call to :*pk_hash* 
> instead of :*qualified_pk_hash*
>
>
> https://github.com/jeremyevans/sequel/blob/588c5c65350b7f5aa82b6eb9e7115eaa6f51d8fb/lib/sequel/model/associations.rb#L2535
>
> The same can be replicated as below:
>
> Employee.dataset.where(@employee.pk_hash) throws the exception for :id 
> being ambiguous
>
> Is there any reason why pk_hash is used in Dataset#where query instead of 
> :qualified_pk_hash? I've overridden (patch work) the pk_hash method to 
> return the qualified_pk_hash to make it work.
>

Which version of Sequel are you using?  Sequel 5 shouldn't have these 
issues as all model classes that use joined datasets wrap the dataset in a 
subquery.  Here's a self contained example that shows correct behavior when 
used on the master branch:

DB.create_table! :users do
  primary_key :id
  Integer :department_id
  String :name
  String :type
end

DB.create_table! :employees do
  primary_key :id
  String :position
end

DB.create_table! :departments do
  primary_key :id
  String :name
end

class User < Sequel::Model
  plugin :class_table_inheritance, key: :type
end

class Employee < User
  many_to_one :department
end

class Department < Sequel::Model
  one_to_many :employees
end

d = Department.create(:name=>'c')
e = Employee.create(:name=>'a', :position=>'b', :department=>d)
p d.employees
d.remove_employee(e)
p d.employees(:reload=>true)
p Employee.dataset.where(e.pk_hash).all

SQL queries used for the remove_employee call are:

UPDATE "users" SET "department_id" = NULL, "name" = 'a', "type" = 
'Employee' WHERE ("id" = 1)
UPDATE "employees" SET "position" = 'b' WHERE ("id" = 1)

SQL query used for the direct pk_hash usage is:

SELECT * FROM (SELECT "users"."id", "users"."department_id", 
"users"."name", "users"."type", "employees"."position" FROM "users" INNER 
JOIN "employees" ON ("employees"."id" = "users"."id")) AS "users" WHERE 
("id" = 1)

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to