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.

GetRelationshipAsync returns RelationshipNotFoundException

See original GitHub issue

DESCRIPTION

After upgrading to v4 we noticed that GetRelationshipAsync now throws an exception.

This was working…

await base.GetRelationshipAsync(id, "client-discount-details", cancellationToken);

Current error

image

Relationships seem correct.

image

The entity

image

TIA.

VERSIONS USED

  • JsonApiDotNetCore version: 4.0.0
  • ASP.NET Core version: 3.1
  • Entity Framework Core version: 3.1.10
  • Database provider: Sql Server

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bart-degreedcommented, Jan 1, 2021

Cloning sounds like a separate concern to me that is independent from the JsonApiDotNetCore pipeline. So I would keep it simple. Just add a class that provides the clone operation, taking DbContext as dependency and call that from within your service.

How that can be implemented is described here. I would first execute a LINQ query with .Include()s to fetch the tree in a single operation, using AsNoTracking() and clear out all IDs afterwards, so they are re-assigned on save. Then add the root object to the DbContext and save it.

The reason I believe going through the JsonApiDotNetCore pipeline for the individual steps is not the best solution, is because several parts in the pipeline need to be re-aligned in each step in order for that to work, which can be hard to get right. And to me it seems there is no real benefit in going through all that. The middleware normally analyzes the kind of request and sets context data in IJsonApiRequest, which is consumed from a service method (but also from the query string parsers). So if you get an incoming request for /books, but then in the Get() service method, call base.GetSecondary(...) (which would normally match /shop/1/books), then the context is wrong. So the parsed query strings do not match what the service executes, the wrong resource definition callbacks are invoked etc. To summarize, the expectation that you can just call any base service method from any other service method is unjustified and I wouldn’t go there, unless it has huge benefits.

Fun fact: I’m actually going there for implementing atomic operations, which enables to batch multiple changes in a single request. And for that to work, I need to update IJsonApiContext and ITargetedFields for each operation within the list to make things work.

1reaction
bkoelmancommented, Dec 30, 2020

It looks like you’re passing a hardcoded relationship name “client-discount-details”. This value is only used to produce the error message, as we assume it matches with the incoming HTTP request. If your goal is to fetch a relationship different from the one coming from the request, you’ll also need to set the JsonApiRequest.Relationship property. You can inject the interface and cast to overwrite the property. Get the value using IResourceContextProvider. Hope that helps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async SQLalchemy: accessing eagerly-loaded empty ...
I am using sqlalchemy + asyncpg, and 'selectin' eager loading. I have Person items that have one-to-many relationships with Friends. I insert a ......
Read more >
cypher query returning UnknownError: node [...] not ...
I discovered that some cypher queries using shortest path fail for specific input, while at the same time work well with others.
Read more >
Getting started with continuous integration for Nest.js APIs
Learn how to build RESTful APIs with Nest.js, a Node.js framework built with TypeScript, and automate testing using CircleCI.
Read more >
Implementing CRUD Operations with One-to-Many ...
With this setup, we can easily retrieve all TodoItem entities associated with a particular TodoList entity by querying the todoItems property of ...
Read more >
API with NestJS #75. Many-to-many relationships using raw ...
We look into a more complex relationship called many-to-many and implement it in our NestJS project using raw SQL.
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