Reputation: 46
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. 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.
Upvotes: 0
Views: 234
Reputation: 46
I was able to find a simple solution with the help of a friend:
.whereRaw('? = ANY(users.close_friends)',[user_id])
Upvotes: 1