[英]how to use a where clause to see if an id exists in a postgres array type column
What I'm trying to do is, join an "events" table on two other tables: "users" and "locations" .我想要做的是,在另外两个表上加入一个“事件”表: “用户”和“位置” 。 Every "event" happens at a specific time and on a specific "location" which needs joining in order for the user to know where the event will take place.
每个“事件”都发生在特定时间和特定“位置” ,需要加入以便用户知道事件将在何处发生。 And is also created by a specific "user" .
并且也是由特定的“用户”创建的。 this user happens to have an array of user ids called close_friends .
这个用户碰巧有一个名为close_friends的用户 ID 数组。 The three tables are the following:
三张表如下:
Events table
===================================================
Column | Type |
-----------------------+--------------------------+
id | integer |
location_id | integer |
guests | integer |
hide_from | integer[] |
only_close_friends | boolean |
is_public | boolean |
min_viable_population | integer |
max_viable_population | integer |
created_by | integer |
sport_type | text |
whats_needed | text[] |
happens_at | timestamp with time zone |
created_at | timestamp with time zone |
updated_at | timestamp with time zone |
updated_by | integer |
___________________________________________________
Users table
===========================================
Column | Type |
---------------+--------------------------+
id | integer |
username | text |
password_hash | text |
first_name | text |
last_name | text |
gender | text |
photo | text |
city | text |
bio | text |
height | integer |
sports | text[] |
photos | text[] |
friends | integer[] |
close_friends | integer[] |
scopes | text[] |
verified | boolean |
created_at | timestamp with time zone |
updated_at | timestamp with time zone |
updated_by | integer |
___________________________________________
Locations table
==========================================
Column | Type |
---------------+--------------------------+
id | integer |
name | text |
maps_url | text |
city | text |
region | text |
cost | text |
photo | text |
meta | text |
sport_types | text[] |
girls_allowed | boolean |
verified | boolean |
created_at | timestamp with time zone |
created_by | integer |
updated_at | timestamp with time zone |
updated_by | integer |
Now The user should be able to create an event which is visible only to his/her close friends.现在用户应该能够创建一个只有他/她的密友才能看到的事件。
what I wanna do is fetch only the events that are to be seen by creator's close friends.我想要做的是只获取创作者的密友可以看到的事件。
To give you an overall idea of what I'm trying to achieve, I'd provide you with the code block that looks good but returns an "Operator doesn't exist" error.为了让您全面了解我要实现的目标,我会为您提供看起来不错但返回“运算符不存在”错误的代码块。
const query = db('events')
.join('users', 'users.id', '=', 'events.created_by')
.join('locations', 'locations.id', '=', 'events.location_id')
.select(
'events.id',
'events.location_id',
'locations.name',
'events.guests',
'locations.city',
'events.min_viable_population',
'events.max_viable_population',
'events.created_by',
'events.sport_type',
'events.whats_needed',
'events.happens_at',
'events.created_at',
'users.username',
'users.first_name',
'users.last_name',
'users.photo',
'users.verified'
)
.where({'only_close_friends': true})
.whereIn('close_friends', function () { /////////////////////////
this.select('id') /////////////////////////
.from('users') // The problem is here //
.where({ id: user_id }) /////////////////////////
}) /////////////////////////
"user_id" is the id of the correctly logged in user. “user_id”是正确登录用户的 ID。
在朋友的帮助下,我找到了一个简单的解决方案:
.whereRaw('? = ANY(users.close_friends)',[user_id])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.