`left_outer_join` instead of graph fixed the issue. I was under the 
impression that I always wanted to use graph for the "extra metadata."

Thanks for the quick response.


On Monday, May 3, 2021 at 11:27:32 AM UTC-5 Jeremy Evans wrote:

> On Mon, May 3, 2021 at 9:05 AM Matt Culpepper <[email protected]> wrote:
>
>> Hi, I'm pretty new to Sequel but I'm trying to figure something out:
>>
>> I have this query that limits the dataset to the current user and it 
>> looks kinda like this:
>>
>> ```
>>             Model
>>               .graph(:access_policies,
>>                      { user_id: user.id, resource_id: :first_account_id) 
>> },
>>                      { table_alias: :ap1 })
>>               .graph(:accounts,
>>                      { id: :first_account_id },
>>                      { table_alias: :a1 })
>>               .graph(:access_policies,
>>                      { user_id: user.id, resource_id: :second_account_id 
>> },
>>                      { table_alias: :ap2 })
>>               .graph(:accounts,
>>                      { id: :second_account_id },
>>                      { table_alias: :a2 })
>>               .where(Sequel.lit('ap1.id is not null and a1.id is not 
>> null'))
>>               .or(Sequel.lit('ap2.id is not null and a2.id is not null'))
>> ```
>>
>> There's also some code that handles `include=first_account` in the query 
>> string, which does an .eager_graph on `first_account`.
>>
>> All good, until `dataset.all` gets called -- then I get a `NoMethodError: 
>> undefined method `model' for #<Sequel::Postgres::Dataset: "SELECT * FROM 
>> \"access_policies\"">`
>>
>> I stepped through the code and it seems to be trying to eager load those 
>> non-eager graphed associations within `EagerGraphLoader`
>>
>> It loops through this: `datasets = opts[:graph][:table_aliases]`
>>
>> and `model` is undefined for each of the table_aliases defined by `graph` 
>> (not `eager_graph`)
>>
>> I'm working on a production legacy codebase, so I'm trying to not break 
>> the auto eager graph stuff -- is there a way I can just tell it which model 
>> to use for the `graph` calls? Or to skip those for the EagerGraphLoader? Or 
>> am I doing it all wrong?
>>
>
> Mixing graph and eager_graph together is not supported.  This is not 
> currently documented, but I'll update the eager_graph documentation to 
> reflect it shortly.  It can't really work because of the different 
> expectations of graph returning subhashes (if the graph_each extension is 
> used) and eager_graph returning nested associations.  There's no reason to 
> use Dataset#graph directly without the graph_each extension.
>
> For your particular case, is the eager_graph on first account necessary?  
> If not, you could try switching it to eager.  If it is required, I'm not 
> sure what the graph calls are doing in the example code.  If you just want 
> to filter the dataset, you shouldn't use graph. You could try switching to 
> left_outer_join and see if that fixes it.
>
> 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/7c0724df-6c56-4b0d-8943-e9a184e55124n%40googlegroups.com.

Reply via email to