{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/nova-issues/issues/246","repository_url":"https://api.github.com/repos/laravel/nova-issues","labels_url":"https://api.github.com/repos/laravel/nova-issues/issues/246/labels{/name}","comments_url":"https://api.github.com/repos/laravel/nova-issues/issues/246/comments","events_url":"https://api.github.com/repos/laravel/nova-issues/issues/246/events","html_url":"https://github.com/laravel/nova-issues/issues/246","id":354333737,"node_id":"MDU6SXNzdWUzNTQzMzM3Mzc=","number":246,"title":"Overeager loading...?","user":{"login":"telkins","id":53731,"node_id":"MDQ6VXNlcjUzNzMx","avatar_url":"https://avatars2.githubusercontent.com/u/53731?v=4","gravatar_id":"","url":"https://api.github.com/users/telkins","html_url":"https://github.com/telkins","followers_url":"https://api.github.com/users/telkins/followers","following_url":"https://api.github.com/users/telkins/following{/other_user}","gists_url":"https://api.github.com/users/telkins/gists{/gist_id}","starred_url":"https://api.github.com/users/telkins/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/telkins/subscriptions","organizations_url":"https://api.github.com/users/telkins/orgs","repos_url":"https://api.github.com/users/telkins/repos","events_url":"https://api.github.com/users/telkins/events{/privacy}","received_events_url":"https://api.github.com/users/telkins/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2018-08-27T14:06:59Z","updated_at":"2018-08-28T08:12:52Z","closed_at":null,"author_association":"NONE","body":"I'm not yet 100% sure, but it looks like a Nova resource gets a little overeager when loading.\r\n\r\nI have the following models *and* nova resources:\r\n* organization\r\n* event\r\n* activity\r\n\r\nAn event model *belongs to* an organization model.  That is, an event has a reference to an organization via its `organization_id`.  It also always loads its activity count:\r\n```\r\n    /** @var array */\r\n    protected $withCount = [\r\n        'activities',\r\n    ];\r\n```\r\n\r\nAn activity model *belongs to* an event model.  That is, an activity has a reference to an event via its `event_id`.\r\n\r\nMy activity resource eager loads events:\r\n```\r\n    public static $with = [\r\n        'event',\r\n    ];\r\n```\r\n\r\nIt also has the following field declared:\r\n```\r\n    public function fields(Request $request)\r\n    {\r\n        return [\r\n            // ...\r\n            BelongsTo::make('Event'),\r\n            // ...\r\n```\r\n\r\nMy event resource doesn't eager load anything.  It does, however, have the following *field*:\r\n```\r\n    public function fields(Request $request)\r\n    {\r\n        return [\r\n            // ...\r\n            BelongsTo::make('Organization'), // <-- pay attention to this for later on...\r\n            // ...\r\n```\r\n\r\nWhen I select *an individual activity* from the Nova UI's activities page, it appears to eager load the events, as expected:\r\n```\r\n// from mysql log:\r\nselect `events`.*, (select count(*) from `activities` where `events`.`id` = `activities`.`event_id`) as `activities_count` from `events` order by `events`.`id` desc\r\n```\r\n\r\nThis is good and as expected.\r\n\r\n*However*....it then goes and individually loads the *organization* for all events...!  It looks like it has a single API call to get all of the events.  It likely does this to populate the event dropdown on the edit activity screen.\r\n\r\nThis is where I start to get a little uncertain about what's taking place, exactly.  The log records about 15 queries that look like *exactly* this:\r\n```\r\n`select * from `organizations` where `organizations`.`id` = 1 limit 1\r\n```\r\n\r\n*Then*, after a couple more queries, which appear to be in their own API call because of a query to the user table first, there are what appear to be 460 of the exact same queries:\r\n```\r\n`select * from `organizations` where `organizations`.`id` = 1 limit 1\r\n```\r\n\r\n(I should note that there are exactly 460 events in my database and only a handful of organizations.  Most events belong to one organization and a very small handful belong to a second organization.  So, a small handful of the queries likely look for organization ID = 2.)\r\n\r\nIn fact, I've confirmed that there is a call to the following URL:\r\n```\r\nhttp://homestead.test/nova-api/activities/associatable/event?current=4&first=false&search=&withTrashed=false\r\n```\r\n\r\nNow, this is where it gets interesting and where I believe Nova is getting overeager when loading these resources.  If I remove the following from my organization resource fields array, then I get *zero* queries for organizations:\r\n```\r\n    public function fields(Request $request)\r\n    {\r\n        return [\r\n            // ...\r\n            // BelongsTo::make('Organization'), // <-- removing this line makes *all* the difference\r\n            // ...\r\n```\r\n\r\nSo, I can see why loading an individual activity would load its event as well as all of the possible events to fill the dropdown (no...I haven't made it `searchable`...yet).  I cannot, however, see why declaring an event's fields to include a further relationship (organization) would have any impact on the situation.\r\n\r\nI hope that this information is sufficiently clear.  While I don't mind a workaround here, I think it does reflect a real problem underneath the hood.  If I'm not mistaken, it seems like Nova is very eagerly loading all sorts of resources, even though they aren't used for anything.\r\n\r\nI've also tried to simplify my examples here from my \"real\" code, so I may have made a small mistake and/or overlooked something.  I'll re-read this and simplify my \"real\" code a bit, too.  But, if anyone has any questions or needs more details, I'll be happy to try to provide it for them.  I'll also be happy to try some different things to see what happens.\r\n\r\nThanks in advance...!  :-)"},"comment":{"url":"https://api.github.com/repos/laravel/nova-issues/issues/comments/416492219","html_url":"https://github.com/laravel/nova-issues/issues/246#issuecomment-416492219","issue_url":"https://api.github.com/repos/laravel/nova-issues/issues/246","id":416492219,"node_id":"MDEyOklzc3VlQ29tbWVudDQxNjQ5MjIxOQ==","user":{"login":"telkins","id":53731,"node_id":"MDQ6VXNlcjUzNzMx","avatar_url":"https://avatars2.githubusercontent.com/u/53731?v=4","gravatar_id":"","url":"https://api.github.com/users/telkins","html_url":"https://github.com/telkins","followers_url":"https://api.github.com/users/telkins/followers","following_url":"https://api.github.com/users/telkins/following{/other_user}","gists_url":"https://api.github.com/users/telkins/gists{/gist_id}","starred_url":"https://api.github.com/users/telkins/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/telkins/subscriptions","organizations_url":"https://api.github.com/users/telkins/orgs","repos_url":"https://api.github.com/users/telkins/repos","events_url":"https://api.github.com/users/telkins/events{/privacy}","received_events_url":"https://api.github.com/users/telkins/received_events","type":"User","site_admin":false},"created_at":"2018-08-28T08:12:52Z","updated_at":"2018-08-28T08:12:52Z","author_association":"NONE","body":"@poxin13 Thx for your input.  Now that you mention it, I had one of the worst OS crashes on my MacBook Pro that I've experienced in recent years...and this was after using Nova for a few days.  Not sure if they're related, but I know switching iTerm tabs was either impossible or very, *very* slow....perhaps because of massive memory consumption...?\r\n\r\nAnyway, I wasn't really able to troubleshoot at the time and I didn't consider that it might be related to Nova.  Looking back, I'm not so sure."}}
{"actor":{"display_login":"telkins"}}