question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

marshmallow-jsonapi forcing loading of related data

See original GitHub issue

I’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:closed
  • Created 8 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
danpolandcommented, May 2, 2017
1reaction
danpolandcommented, Mar 23, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found