简体   繁体   English

如何使用 where 子句查看 id 是否存在于 postgres 数组类型列中

[英]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 })                   ///////////////////////// 
      })                                            /////////////////////////

Note笔记

"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.

相关问题 如何查看数组中的id是否已存在于localstorage中 - how to see if id in array already exists in localstorage JavaScript(Postgres DB)-如何在WHERE IN()子句中使用带数组的预备语句作为参数 - JavaScript (Postgres DB) - How to use a prepared statement with an array as parameter in the WHERE IN ( ) clause 我可以在firestore中使用`exists` where子句吗? - Is there an `exists` where clause I can use with firestore? mysql where 子句中的列“id”不明确 - mysql Column 'id' in where clause is ambiguous 当我在对象数组中键入内容时,如何查看字符是否存在于数组的每个实例中? - How do I see if a character exists within each instance of the array as I type within an array of objects? 查看数组的子数组是否存在 - See if subarray of array exists Yii CDbCriteria错误:where子句中的列“ id”不明确 - Yii CDbCriteria error: Column 'id' in where clause is ambiguous 如何使用_.where通过ID查找嵌套在对象数组中的对象? - How to use _.where to find by ID an object nested in an array of objects? 如何使用 uuid 作为 postgres 的默认 ID? - How to use a uuid as default ID with postgres? 如何检查对象数组中是否存在id值 - How to check if an id value exists in an object array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM