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.

Nested Controller parent param filter not working

See original GitHub issue

We are trying to have a nested controller for two objects: ProjectEntity |— InputEntity

When trying to send a GET request we get the following response: column InputEntity.projectId_id._id does not exist

Here is my controller:

@Crud({
  model: {
    type: InputEntity
  },
  params: {
    projectId: {
      field: 'projectId',
      type: 'uuid'
    },
    id: {
      field: '_id',
      type: 'uuid',
      primary: true
    }
  }
})
@ApiTags('inputs')
@Controller('project/:projectId/inputs')
export class InputController {

The entity relation is defined as:

@ManyToOne(
    type => ProjectEntity,
    (project: ProjectEntity) => project.inputs,
    { onDelete: 'CASCADE' }
  )
  @ApiPropertyOptional()
  projectId: ProjectEntity;

We had this code working a few weeks ago, but we tried multiple versions of this package, typeorm and postgres and can’t get it to work again.

After digging in into the code of NestJSX/curd we found that the resulting query has:

... WHERE ("InputEntity"."projectId_id._id" = $1)

And we try to execute it in a SQL editor and it is not a valid query. Without the ._id part the query works.

The strange field comes from entityColumnsHash where in the initialization it thinks it is an embedded type (but it is stored as a simple ID string)

We will be thankful if this problem will be resolved as we can’t find a way to deploy our system in the present time.

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
michaelyalicommented, May 21, 2020

Yes, while fixing filtering by relations and especially nested relations, I’ve changed a join relation internal mechanism, and actually I’m really surprised why this thing that you’ve described worked earlier because it looks wrong to me though - you’ve trying to filter by a relation name directly but not by either an entity column or a joint relation column name. Again, it’s very weird and not an intended behavior but rather a side effect of the bug we had earlier.

So, I see two possible solutions here. You can do one of these:

  1. First option: add a projectId_id column to the entity:
@Column()
projectId_id: string

and change your param field name in the controller:

projectId: {
    field: 'projectId_id',
    type: 'uuid'
},
  1. Second option: join this projectId relation and filter by its _id column:
@Crud({
  model: {
    type: InputEntity
  },
  query: {
    join: {
      projectId: {} // join this relation
    }
  },
  params: {
    projectId: {
      field: 'projectId._id', // filter by a joint relation column 
      type: 'uuid'
    },
    id: {
      field: '_id',
      type: 'uuid',
      primary: true
    }
  }
})

Also, let me notice something - usually, a relation name in your case would be called project but not projectId which confused me a little bit when I first saw this.

0reactions
avmraacommented, May 21, 2020

Encountered the same issue - different routes worked and failed depending on the params field mapping. The workaround is to define 2 controllers for the same path with different routes options:

@Crud({
  model: {
    type: Preference,
  },
  params: {
    clientId: {
      field: 'client',
      type: 'number',
      primary: false,
    },
  },
  query: {
    join: {
      client: {
        eager: true,
        select: false,
      },
    },
  },
  routes: {
    only: ['createManyBase', 'createOneBase', 'replaceOneBase'],
  },
})
@Controller('clients/:clientId/preferences')
export class PreferencesController1 implements CrudController<Preference> {
  constructor(public service: PreferenceService) {}
}

@Crud({
  model: {
    type: Preference,
  },
  params: {
    clientId: {
      field: 'client.id',
      type: 'number',
      primary: false,
    },
  },
  query: {
    join: {
      client: {
        eager: true,
        select: false,
      },
    },
  },
  routes: {
    only: ['getOneBase', 'getManyBase', 'deleteOneBase', 'updateOneBase'],
  },
})
@Controller('clients/:clientId/preferences')
export class PreferencesController2 implements CrudController<Preference> {
  constructor(public service: PreferenceService) {}
}

looks ugly but works. This is apparently a bug in the library.

Read more comments on GitHub >

github_iconTop Results From Across the Web

seach filter inside a parent ng-repeat not working
Quick solution here would be to explicitly initialise q7 in your controller to ensure no variable shadowing occurs. Included a stripped down ...
Read more >
Permitting Nested Arrays using Strong Params in Rails
Strong Params allow developers to specify in the controller which parameters are accepted and used. By permitting only the expected params, ...
Read more >
Top 18 Most Common AngularJS Developer Mistakes - Toptal
Seeing work in progress - and the controller is already executed, so it actually is progress - is better than having the app...
Read more >
Create a nested navigation flow - Flutter documentation
Gif showing the nested "setup" flow. In this recipe, you implement a four-page IoT setup flow that maintains its own navigation nested beneath...
Read more >
Navigating with Compose - Android Developers
Note: If you are not familiar with Compose, review the Jetpack Compose resources ... The NavController is the central API for the Navigation...
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