问题是->运算符返回json,这就是为什么要返回一个字符串,并将引号作为字符串的一部分,而不是预期的字符串。详见
https://www.postgresql.org/docs/9.5/functions-json.html
.
您应该改用->>运算符,因为它将返回整数或文本值。返回的文本将不包含引号。然后,您可以将文本转换为uuid,postgres将识别它。
SQL小提琴:
http://sqlfiddle.com/#!17/d9e06/3/0
create table sessions (id integer, data json);
insert into sessions values(1, ' { "user_id": "bc2166dd-ca1d-41c6-9c66-610f844ca139" }');
create table users (id uuid, email text);
insert into users values('bc2166dd-ca1d-41c6-9c66-610f844ca139','a@b.com');
--query using ->> operator to return an integer or text instead of JSON
select (data->>'user_id')::uuid
, *
from sessions
join users on (data->>'user_id')::uuid = users.id;