.load(relations) not loading models
See original GitHub issueIntroduction
I’m doing a
model.load(['role.event', 'user']).then(m => m.related('role').related('event'))
and the event is coming up as an empty event Model. However the event definitively exists and the foreign keys are defined. I’m doing this in the created
event on the model, if that could be the source of the issue?
Steps to reproduce issue
class Event extends Bookshelf.Model {
roles() {
return this.hasMany('EventRole', 'event_id');
}
}
class EventRole extends Bookshelf.Model {
event(){
return this.belongsTo('Event', 'event_id');
}
applications() {
return this.hasMany('EventApplication', 'event_role_id');
}
}
class EventApplication extends Bookshelf.Model {
role() {
return this.belongsTo('EventRole', 'event_role_id');
}
initialize(){
this.on('created', model => model.load(['role.event', 'user']).then(m => m.related('role').related('event')), this)
}
Expected behaviour
To load the event model
Actual behaviour
Doesn’t load the event model.
EventApplication {
attributes:
{ event_role_id: 24,
user_id: 1,
updated_at: 2017-09-18T13:39:27.777Z,
created_at: 2017-09-18T13:39:27.777Z,
id: 25 },
_previousAttributes:
{ event_role_id: 24,
user_id: 1,
updated_at: 2017-09-18T13:39:27.777Z,
created_at: 2017-09-18T13:39:27.777Z,
id: 25 },
changed: {},
relations:
{ role:
EventRole {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: {},
cid: 'c8146',
id: 24,
_events: [Object],
_eventsCount: 2,
relatedData: [Object] },
user:
User {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: {},
cid: 'c8149',
id: 1,
_events: [Object],
_eventsCount: 2,
virtuals: [Object],
relatedData: [Object] } },
cid: 'c8124',
_events: { created: [Function], updating: [Function] },
_eventsCount: 2,
_knex: null,
id: 25 }
EventRole {
attributes:
{ id: 24,
event_id: 54,
title: 'role 2',
about: null,
capacity: null,
created_at: 2017-09-18T13:39:23.919Z,
updated_at: 2017-09-18T13:39:23.919Z,
date_starting: null,
date_ending: null,
deleted_at: null },
_previousAttributes:
{ id: 24,
event_id: 54,
title: 'role 2',
about: null,
capacity: null,
created_at: 2017-09-18T13:39:23.919Z,
updated_at: 2017-09-18T13:39:23.919Z,
date_starting: null,
date_ending: null,
deleted_at: null },
changed: {},
relations: {},
cid: 'c8146',
id: 24,
_events:
{ fetching: [Function: bound skipDeleted],
'fetching:collection': [Function: bound skipDeleted] },
_eventsCount: 2,
relatedData:
RelationBase {
targetTableName: 'event_roles',
targetIdAttribute: 'id',
type: 'belongsTo',
target: { [Function: EventRole] dependents: [Array] },
foreignKey: 'event_role_id',
foreignKeyTarget: undefined,
parentId: 25,
parentTableName: 'event_applications',
parentIdAttribute: 'event_role_id',
parentAttributes:
{ event_role_id: 24,
user_id: 1,
updated_at: 2017-09-18T13:39:27.777Z,
created_at: 2017-09-18T13:39:27.777Z,
id: 25 },
parentFk: 24 } }
Event {
attributes: {},
_previousAttributes: {},
changed: {},
relations: {},
cid: 'c8168',
_events:
{ publish: [Function],
updating: [Function],
destroying: [Function] },
_eventsCount: 3,
relatedData:
RelationBase {
targetTableName: 'events',
targetIdAttribute: 'id',
type: 'belongsTo',
target: { [Function: Event] dependents: [Array] },
foreignKey: 'event_id',
foreignKeyTarget: undefined,
parentId: 24,
parentTableName: 'event_roles',
parentIdAttribute: 'event_id',
parentAttributes:
{ id: 24,
event_id: 54,
title: 'role 2',
about: null,
capacity: null,
created_at: 2017-09-18T13:39:23.919Z,
updated_at: 2017-09-18T13:39:23.919Z,
date_starting: null,
date_ending: null,
deleted_at: null },
parentFk: 54 } }
The strange thing is the load gets called for 3 EventApplications and the last one loads the event, the others dont.
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Laravel model belongs to relationship loading issue
When accessing Eloquent relationships as properties, the relationship data is "lazy loaded". This means the relationship data is not ...
Read more >Eager loading a model method that is not a relationship?
We are doing a bunch of optimization and trying to cut down on sql queries. Every time you do... $model->myMethod() it does an...
Read more >Eloquent: Relationships
Dynamic properties are "lazy loading", meaning they will only load their relationship data when you actually access them. Because of this, developers often...
Read more >Eloquent Relationship Not Loading When Called For ...
Do I need to tweak mysql settings or something? I want to load relationship data for 20.000 items, and each item can have...
Read more >Eager Load Relationships on an Existing Model in Laravel
So, here is the problem. How to load relationships on already-retrieved models like above? The solution is Lazy Eager Loading. Use the load() ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
seems you can’t await a transaction. Makes sense
If the model was created inside a transaction, the
created
event can fire before the transaction has completed. Pass through the transacting in the 3rd argument and await the promise to fix this