Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ambiguous column reference \"id\" when using $joinRelation and $select together #38

Closed
Aerlin opened this issue Nov 27, 2018 · 5 comments

Comments

@Aerlin
Copy link

Aerlin commented Nov 27, 2018

I'm using $select to include a custom sql function in my comments query.

// Default $select in before hook.
context.params.query["$select"] = context.params.query["$select"] || [
	'comments.*', 
	raw('comment_hotness(comments.score, comments.created_at) as hotness')
]

Which works well.

A problem I'm having is when I try to filter based on a relationship value, I get the ambiguous column name error.

// Default $joinRelation in before hook.
context.params.query["$joinRelation"] = context.params.query["$joinRelation"] || "[tags]";

A test query.

http://xxx/comments?tags.tag=abc

Error that's returned.

Error in 'comments' service method 'find' Forbidden: select distinct "comments".*, comment_hotness(comments.score, comments.created_at) as hotness, "id", "comments".* from "comments" inner join "comment_tags" as "tags" on "tags"."comment_id" = "comments"."id" where "tags"."tag" = $1 limit $2 - column reference "id" is ambiguous

In feathers-object index.js, adding the table's name to line 240 seems to fix it.

    if (filters.$select) {
      q = q.select(...filters.$select.concat(`${this.Model.tableName}.${this.id}`));
    } // $eager for Objection eager queries

The new sql query.

select distinct "comments".*, comment_hotness(comments.score, comments.created_at) as hotness, "comments"."id", "comments".* from "comments" inner join "comment_tags" as "tags" on "tags"."comment_id" = "comments"."id"

The query then executes as expected.

@dekelev
Copy link
Member

dekelev commented Nov 28, 2018

You should use $pick instead of $select when using $eager. it behaves different, but ends up with the same results.

@dekelev dekelev closed this as completed Nov 28, 2018
@Aerlin
Copy link
Author

Aerlin commented Nov 29, 2018

$pick returns a list of empty objects and doesn't call the custom SQL function in the query. Again, I'm using $select to call a custom function (comment_hotness) in my db.

context.params.query["$pick"] = [
		'comments.*', 
		raw('comment_hotness(comments.score, comments.created_at) as hotness')
	];

SQL query

select distinct "comments".* from "comments" inner join "comment_tags" as "tags" on "tags"."comment_id" = "comments"."id"

Results

{
  "total": 11,
  "limit": 20,
  "skip": 0,
  "data": [
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {}
  ]
}

@dekelev dekelev reopened this Nov 29, 2018
@dekelev
Copy link
Member

dekelev commented Nov 29, 2018

Thanks for sharing this. I'll try to debug this on the weekend.

@dekelev
Copy link
Member

dekelev commented Nov 30, 2018

Thanks @Aerlin, your suggested bug fix is published in v1.3.1

@dekelev dekelev closed this as completed Nov 30, 2018
@Aerlin
Copy link
Author

Aerlin commented Nov 30, 2018

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants