On Tue, Oct 19, 2021 at 4:26 PM [email protected] <
[email protected]> wrote:

> Thanks for getting back to me!
>
> Assuming you meant to use lazy attributes in both the parent class and the
> subclass, it semi-worked in that I no longer see the missing column errors
> on the subclass. However, we still don't have typecasting and it seems I
> can't update the lazy loaded column from the subclass.
>
> Here is my setup using the same DB schema as before
>
> class Employee < Sequel::Model
>   plugin :lazy_attributes, :bio
>   plugin :class_table_inheritance, key: :kind
> end
>
> class Manager < Employee
>   old_from = dataset.opts[:from][0]
>   set_dataset
> dataset.from(old_from.expression.select_append{employees[:bio]}.as(old_from.alias))
>   lazy_attributes :bio
> end
>
> *Employee works as expected with typecasting and setting the lazy loaded
> attribute*
>
> employee = Employee.create active: 0, bio: "Superclass bio"
> puts "employee's biography: #{employee.bio}"
>
>
>
> SQL
>
> INSERT INTO "employees" ("active", "bio", "kind") VALUES (false,
> 'Superclass bio', 'Employee') RETURNING "id"
> SELECT "employees"."id", "employees"."kind", "employees"."active" FROM
> "employees" WHERE ("id" = 1) LIMIT 1
> SELECT "employees"."bio" FROM "employees" WHERE ("id" = 1) LIMIT 1
>
> *Manager still doesn't work quite right. The lazy loading works but bio is
> never inserted*
>
> manager = Manager.create active: false, bio: "Manager bio"
> manager.reload
> puts %Q{manager's biography is "Manager bio": #{manager.bio == "Manager
> bio"}}
>
> SQL
>
> INSERT INTO "employees" ("kind", "active") VALUES ('Manager', false)
> RETURNING *
> INSERT INTO "managers" ("id") VALUES (2) RETURNING *
> SELECT "employees"."id", "employees"."kind", "employees"."active" FROM
> (SELECT "employees"."id", "employees"."kind", "employees"."active",
> "employees"."bio" FROM "employees" INNER JOIN "managers" ON
> ("managers"."id" = "employees"."id")) AS "employees" WHERE ("id" = 2) LIMIT
> 1
> SELECT "employees"."bio" FROM (SELECT "employees"."id",
> "employees"."kind", "employees"."active", "employees"."bio" FROM
> "employees" INNER JOIN "managers" ON ("managers"."id" = "employees"."id"))
> AS "employees" WHERE ("id" = 2) LIMIT 1
>
>
> *Typecasting is also not working for the subclass*
>
> Manager.create active: 0
>
> ERROR -- : PG::DatatypeMismatch: ERROR:  column "active" is of type
> boolean but expression is of type integer
> LINE 1: ..."employees" ("kind", "active") VALUES ('Manager', 0)
> RETURNI...                                                          ^
> HINT:  You will need to rewrite or cast the expression.: INSERT INTO
> "employees" ("kind", "active") VALUES ('Manager', 0) RETURNING *
> manager = Manager.create active: false, bio: "Manager bio"
>
>
> I suspect it's because the schema and columns for Manager are missing the
> bio column.
>
> irb(main):001:0> Manager.db_schema
> => {:id=>{}, :kind=>{}, :active=>{}}
> irb(main):002:0> Manager.columns
> => [:id, :kind, :active]
>

Sorry for the delay in responding.  I've tested the following and it should
handle your case:

 class Employee < Sequel::Model
  plugin :lazy_attributes, :bio
  plugin :class_table_inheritance, key: :kind
  cti_table_columns << :bio
end

class Manager < Employee
  old_from = dataset.opts[:from][0]
  @dataset =
dataset.from(old_from.expression.select_append{employees[:bio]}.as(old_from.alias))
end

If you continue to run into problems, please let me know.

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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CADGZSSdNJrDeekjuNDBE7aMYphQ26MA%2Bmbge8UXAWs0GKNFimw%40mail.gmail.com.

Reply via email to