Ambiguous column reference using a JSON operator on PostgreSQL
I can't find a valid syntax to refer a JSON field on a PostgreSQL function.
I'm using a function to call some values from all my clients schemas, one of it is the rule_id, stored on a JSON type column. As show below:
RETURN QUERY EXECUTE 'SELECT
r.id, r.created, r.type, r.asset_type, r.title, r.amount, r.earning_type, a.user_id, u.customer_id,
r.rule ->>' || 'id' || ' "rule_id"
FROM ' || schemaName || '.reward AS r RIGHT JOIN ' || schemaName || '.avatar AS a ON r.avatar_id = a.id,
_global.user AS u WHERE u.id = a.user_id
'
;
The JSON field have this structure, and the id is on the main path (ex. Id:93):
object {13}
updated : null
notificationRules [0]
name : Level rule
script : null
levelStart : 500
created : 2018-09-18T20:25:27.488+0000
postRule {12}
id : 93
gamificationEventId : platform.reward.XPChangedEvent
gamification {9}
levelIndex : 2
levelEnd : 1999
But it returns the following error:
ERROR: column reference "id" is ambiguous
LINE 3: r.rule ->>id "rule_id"
Does anyone have an idea how to make this work?
json postgresql function
add a comment |
I can't find a valid syntax to refer a JSON field on a PostgreSQL function.
I'm using a function to call some values from all my clients schemas, one of it is the rule_id, stored on a JSON type column. As show below:
RETURN QUERY EXECUTE 'SELECT
r.id, r.created, r.type, r.asset_type, r.title, r.amount, r.earning_type, a.user_id, u.customer_id,
r.rule ->>' || 'id' || ' "rule_id"
FROM ' || schemaName || '.reward AS r RIGHT JOIN ' || schemaName || '.avatar AS a ON r.avatar_id = a.id,
_global.user AS u WHERE u.id = a.user_id
'
;
The JSON field have this structure, and the id is on the main path (ex. Id:93):
object {13}
updated : null
notificationRules [0]
name : Level rule
script : null
levelStart : 500
created : 2018-09-18T20:25:27.488+0000
postRule {12}
id : 93
gamificationEventId : platform.reward.XPChangedEvent
gamification {9}
levelIndex : 2
levelEnd : 1999
But it returns the following error:
ERROR: column reference "id" is ambiguous
LINE 3: r.rule ->>id "rule_id"
Does anyone have an idea how to make this work?
json postgresql function
If you were typing this query by hand, what would ther.rule ->>id "rule_id"part look like? Right now,idis an unquoted identifier so PostgreSQL is looking for anidcolumn and finding several options,"rule_id"is a quoted identifier so once PostgreSQL can figure out whatidmeans it will start looking for arule_idcolumn. Then it will get confused again because it won't know what two identifiers beside each other means.
– mu is too short
Nov 23 '18 at 20:17
Also, you might want to have a look at theformatfunction;formatis safer, cleaner, and much easier to read than string concatenation.
– mu is too short
Nov 23 '18 at 20:19
Ifr.rule ->>' || 'id' || ' "rule_id"is attempting to use the value ofr.idas the field name or index to read, you should qualify it:r.rule ->>' || 'r.id' || ' "rule_id". If not, and you're trying to access a json key called "id", then it needs to be in single quotes. And in either case there is no reason to use concatenation, it should be a string literal:r.rule ->>''id'' "rule_id"
– 404
Nov 23 '18 at 22:10
thanks everybody. i've used:RETURN QUERY EXECUTE E'SELECT r.id, r.created, r.type, r.asset_type, r.title, r.amount, r.earning_type, a.user_id, u.customer_id, r.rule ->>'id' "rule_id" FROM ' || schemaName || '.reward AS r RIGHT JOIN ' || schemaName || '.avatar AS a ON r.avatar_id = a.id RIGHT JOIN _global.user AS u ON u.id = a.user_id
– Gabriela Peluso
Nov 26 '18 at 18:27
add a comment |
I can't find a valid syntax to refer a JSON field on a PostgreSQL function.
I'm using a function to call some values from all my clients schemas, one of it is the rule_id, stored on a JSON type column. As show below:
RETURN QUERY EXECUTE 'SELECT
r.id, r.created, r.type, r.asset_type, r.title, r.amount, r.earning_type, a.user_id, u.customer_id,
r.rule ->>' || 'id' || ' "rule_id"
FROM ' || schemaName || '.reward AS r RIGHT JOIN ' || schemaName || '.avatar AS a ON r.avatar_id = a.id,
_global.user AS u WHERE u.id = a.user_id
'
;
The JSON field have this structure, and the id is on the main path (ex. Id:93):
object {13}
updated : null
notificationRules [0]
name : Level rule
script : null
levelStart : 500
created : 2018-09-18T20:25:27.488+0000
postRule {12}
id : 93
gamificationEventId : platform.reward.XPChangedEvent
gamification {9}
levelIndex : 2
levelEnd : 1999
But it returns the following error:
ERROR: column reference "id" is ambiguous
LINE 3: r.rule ->>id "rule_id"
Does anyone have an idea how to make this work?
json postgresql function
I can't find a valid syntax to refer a JSON field on a PostgreSQL function.
I'm using a function to call some values from all my clients schemas, one of it is the rule_id, stored on a JSON type column. As show below:
RETURN QUERY EXECUTE 'SELECT
r.id, r.created, r.type, r.asset_type, r.title, r.amount, r.earning_type, a.user_id, u.customer_id,
r.rule ->>' || 'id' || ' "rule_id"
FROM ' || schemaName || '.reward AS r RIGHT JOIN ' || schemaName || '.avatar AS a ON r.avatar_id = a.id,
_global.user AS u WHERE u.id = a.user_id
'
;
The JSON field have this structure, and the id is on the main path (ex. Id:93):
object {13}
updated : null
notificationRules [0]
name : Level rule
script : null
levelStart : 500
created : 2018-09-18T20:25:27.488+0000
postRule {12}
id : 93
gamificationEventId : platform.reward.XPChangedEvent
gamification {9}
levelIndex : 2
levelEnd : 1999
But it returns the following error:
ERROR: column reference "id" is ambiguous
LINE 3: r.rule ->>id "rule_id"
Does anyone have an idea how to make this work?
json postgresql function
json postgresql function
edited Nov 23 '18 at 21:06
marc_s
573k12811071255
573k12811071255
asked Nov 23 '18 at 19:18
Gabriela PelusoGabriela Peluso
11
11
If you were typing this query by hand, what would ther.rule ->>id "rule_id"part look like? Right now,idis an unquoted identifier so PostgreSQL is looking for anidcolumn and finding several options,"rule_id"is a quoted identifier so once PostgreSQL can figure out whatidmeans it will start looking for arule_idcolumn. Then it will get confused again because it won't know what two identifiers beside each other means.
– mu is too short
Nov 23 '18 at 20:17
Also, you might want to have a look at theformatfunction;formatis safer, cleaner, and much easier to read than string concatenation.
– mu is too short
Nov 23 '18 at 20:19
Ifr.rule ->>' || 'id' || ' "rule_id"is attempting to use the value ofr.idas the field name or index to read, you should qualify it:r.rule ->>' || 'r.id' || ' "rule_id". If not, and you're trying to access a json key called "id", then it needs to be in single quotes. And in either case there is no reason to use concatenation, it should be a string literal:r.rule ->>''id'' "rule_id"
– 404
Nov 23 '18 at 22:10
thanks everybody. i've used:RETURN QUERY EXECUTE E'SELECT r.id, r.created, r.type, r.asset_type, r.title, r.amount, r.earning_type, a.user_id, u.customer_id, r.rule ->>'id' "rule_id" FROM ' || schemaName || '.reward AS r RIGHT JOIN ' || schemaName || '.avatar AS a ON r.avatar_id = a.id RIGHT JOIN _global.user AS u ON u.id = a.user_id
– Gabriela Peluso
Nov 26 '18 at 18:27
add a comment |
If you were typing this query by hand, what would ther.rule ->>id "rule_id"part look like? Right now,idis an unquoted identifier so PostgreSQL is looking for anidcolumn and finding several options,"rule_id"is a quoted identifier so once PostgreSQL can figure out whatidmeans it will start looking for arule_idcolumn. Then it will get confused again because it won't know what two identifiers beside each other means.
– mu is too short
Nov 23 '18 at 20:17
Also, you might want to have a look at theformatfunction;formatis safer, cleaner, and much easier to read than string concatenation.
– mu is too short
Nov 23 '18 at 20:19
Ifr.rule ->>' || 'id' || ' "rule_id"is attempting to use the value ofr.idas the field name or index to read, you should qualify it:r.rule ->>' || 'r.id' || ' "rule_id". If not, and you're trying to access a json key called "id", then it needs to be in single quotes. And in either case there is no reason to use concatenation, it should be a string literal:r.rule ->>''id'' "rule_id"
– 404
Nov 23 '18 at 22:10
thanks everybody. i've used:RETURN QUERY EXECUTE E'SELECT r.id, r.created, r.type, r.asset_type, r.title, r.amount, r.earning_type, a.user_id, u.customer_id, r.rule ->>'id' "rule_id" FROM ' || schemaName || '.reward AS r RIGHT JOIN ' || schemaName || '.avatar AS a ON r.avatar_id = a.id RIGHT JOIN _global.user AS u ON u.id = a.user_id
– Gabriela Peluso
Nov 26 '18 at 18:27
If you were typing this query by hand, what would the
r.rule ->>id "rule_id" part look like? Right now, id is an unquoted identifier so PostgreSQL is looking for an id column and finding several options, "rule_id" is a quoted identifier so once PostgreSQL can figure out what id means it will start looking for a rule_id column. Then it will get confused again because it won't know what two identifiers beside each other means.– mu is too short
Nov 23 '18 at 20:17
If you were typing this query by hand, what would the
r.rule ->>id "rule_id" part look like? Right now, id is an unquoted identifier so PostgreSQL is looking for an id column and finding several options, "rule_id" is a quoted identifier so once PostgreSQL can figure out what id means it will start looking for a rule_id column. Then it will get confused again because it won't know what two identifiers beside each other means.– mu is too short
Nov 23 '18 at 20:17
Also, you might want to have a look at the
format function; format is safer, cleaner, and much easier to read than string concatenation.– mu is too short
Nov 23 '18 at 20:19
Also, you might want to have a look at the
format function; format is safer, cleaner, and much easier to read than string concatenation.– mu is too short
Nov 23 '18 at 20:19
If
r.rule ->>' || 'id' || ' "rule_id" is attempting to use the value of r.id as the field name or index to read, you should qualify it: r.rule ->>' || 'r.id' || ' "rule_id". If not, and you're trying to access a json key called "id", then it needs to be in single quotes. And in either case there is no reason to use concatenation, it should be a string literal: r.rule ->>''id'' "rule_id"– 404
Nov 23 '18 at 22:10
If
r.rule ->>' || 'id' || ' "rule_id" is attempting to use the value of r.id as the field name or index to read, you should qualify it: r.rule ->>' || 'r.id' || ' "rule_id". If not, and you're trying to access a json key called "id", then it needs to be in single quotes. And in either case there is no reason to use concatenation, it should be a string literal: r.rule ->>''id'' "rule_id"– 404
Nov 23 '18 at 22:10
thanks everybody. i've used:
RETURN QUERY EXECUTE E'SELECT r.id, r.created, r.type, r.asset_type, r.title, r.amount, r.earning_type, a.user_id, u.customer_id, r.rule ->>'id' "rule_id" FROM ' || schemaName || '.reward AS r RIGHT JOIN ' || schemaName || '.avatar AS a ON r.avatar_id = a.id RIGHT JOIN _global.user AS u ON u.id = a.user_id – Gabriela Peluso
Nov 26 '18 at 18:27
thanks everybody. i've used:
RETURN QUERY EXECUTE E'SELECT r.id, r.created, r.type, r.asset_type, r.title, r.amount, r.earning_type, a.user_id, u.customer_id, r.rule ->>'id' "rule_id" FROM ' || schemaName || '.reward AS r RIGHT JOIN ' || schemaName || '.avatar AS a ON r.avatar_id = a.id RIGHT JOIN _global.user AS u ON u.id = a.user_id – Gabriela Peluso
Nov 26 '18 at 18:27
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451955%2fambiguous-column-reference-using-a-json-operator-on-postgresql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451955%2fambiguous-column-reference-using-a-json-operator-on-postgresql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you were typing this query by hand, what would the
r.rule ->>id "rule_id"part look like? Right now,idis an unquoted identifier so PostgreSQL is looking for anidcolumn and finding several options,"rule_id"is a quoted identifier so once PostgreSQL can figure out whatidmeans it will start looking for arule_idcolumn. Then it will get confused again because it won't know what two identifiers beside each other means.– mu is too short
Nov 23 '18 at 20:17
Also, you might want to have a look at the
formatfunction;formatis safer, cleaner, and much easier to read than string concatenation.– mu is too short
Nov 23 '18 at 20:19
If
r.rule ->>' || 'id' || ' "rule_id"is attempting to use the value ofr.idas the field name or index to read, you should qualify it:r.rule ->>' || 'r.id' || ' "rule_id". If not, and you're trying to access a json key called "id", then it needs to be in single quotes. And in either case there is no reason to use concatenation, it should be a string literal:r.rule ->>''id'' "rule_id"– 404
Nov 23 '18 at 22:10
thanks everybody. i've used:
RETURN QUERY EXECUTE E'SELECT r.id, r.created, r.type, r.asset_type, r.title, r.amount, r.earning_type, a.user_id, u.customer_id, r.rule ->>'id' "rule_id" FROM ' || schemaName || '.reward AS r RIGHT JOIN ' || schemaName || '.avatar AS a ON r.avatar_id = a.id RIGHT JOIN _global.user AS u ON u.id = a.user_id– Gabriela Peluso
Nov 26 '18 at 18:27