#40779 will be caused if (1) having nested where condition for through
association, and (2) through scope has references values, and (3) the
model has no association which is the same name with table name.
In that case, the join root will be found by the table name but the join
root isn't related with association reflection.
This changes to return the table klass from join dependency tree, it
works regardless of whether the join root or join association children.
Fixes#40779.
Currently custom attributes are always qualified by the table name in
the generated SQL wrongly even if the table doesn't have the named
column, it would cause an invalid SQL error.
Custom attributes should only be qualified if the table has the same
named column.