marshmallow-jsonapi forcing loading of related data
See original GitHub issueI’m using marshmallow-jsonapi with sqlalchemy, and it’s forcing loading of related data, when all I want is for marshmallow-json to create a link to related data. Here’s a snippet of what I’m doing:
class Server(db.Model):
__tablename__ = 'servers'
id = db.Column(db.Integer,
primary_key=True)
hostname = db.Column(db.String(255, collation='utf8_bin'),
unique=True,
nullable=False)
jobs = db.relationship('Job', back_populates='server')
class Job(db.Model):
__tablename__ = 'jobs'
id = db.Column(db.Integer,
primary_key=True)
server_id = db.Column(db.Integer, db.ForeignKey('servers.id'),
nullable=False)
name = db.Column(db.String(255, collation='utf8_bin'),
index=True,
nullable=False)
server = db.relationship('Server', back_populates='jobs')
class JobSchema(Schema):
id = fields.Integer(dump_only=True)
name = fields.String()
server = Relationship(
related_view='.server_detail',
related_view_kwargs={'server_id': '<server_id>'},
include_data=False,
)
class Meta:
type_ = 'jobs'
data = JobSchema().dump(Job.query.all(), many=True).data
Rendering these jobs triggers an access to a job’s server
field, which triggers a SQL query to fetch the server, even though the fetched server record is never used. It would be great if marshmallow-jsonapi was able to suppress marshmallow accessing this field.
As a workaround, I can cheat the relationship, and just add a attribute="id"
to the Relationship
constructor, which I know will always be present on the record. It’s not ideal, but it works.
Issue Analytics
- State:
- Created 8 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Loading Related Data - EF Core - Microsoft Learn
Lazy loading means that the related data is transparently loaded from the database when the navigation property is accessed.
Read more >Relationship Loading Techniques
Lazy loading refers to objects are returned from a query without the related objects loaded at first. When the given collection or reference ......
Read more >Prevent AutoMapper Projections From Forcing Related Data ...
I've disabled lazy loading for my context, and I want to conditionally load related data for particular entities.
Read more >Lazy loading - LLBLGen Pro Runtime Framework v5.2 ...
Another way to force loading of a related entity or collection is by ... Forcing a fetch has a difference with AlwaysFetchFieldMappedOnRelation in...
Read more >Working with Related Data in EF Core 6 - Aaron Bos
So, how can eager loading help us populate the navigation properties with data? The answer is the Include method. There are two overloads...
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 FreeTop 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
Top GitHub Comments
The related PR: https://github.com/marshmallow-code/marshmallow-jsonapi/pull/82
You still might run into an issue where you want to allow includes to follow the relationship when there is a
include=my_relation
query string in the request but the rest of the time you just want to use the foreign key. I have a solution for this problem that I will eventually PR.