【问题标题】:how to use a where clause to see if an id exists in a postgres array type column如何使用 where 子句查看 id 是否存在于 postgres 数组类型列中
【发布时间】:2021-01-11 13:32:23
【问题描述】:

我要做的是,将 "events" 表加入另外两个表:"users""locations" >。 每个“事件”都发生在特定的时间和特定的“位置”,需要加入这些事件才能让用户知道事件将在哪里发生。并且也是由特定的“用户”创建的。这个用户恰好有一个名为 close_friends 的用户 ID 数组。三张表如下:

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                  |

现在用户应该能够创建一个只有他/她的亲密朋友才能看到的事件。
我想做的是只获取创作者的密友可以看到的事件。
为了让您全面了解我想要实现的目标,我将为您提供看起来不错但返回 “运算符不存在” 错误的代码块。

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”是正确登录用户的id。

【问题讨论】:

    标签: javascript node.js arrays postgresql knex.js


    【解决方案1】:

    在朋友的帮助下,我找到了一个简单的解决方案:

    .whereRaw('? = ANY(users.close_friends)',[user_id])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      • 2018-03-06
      • 1970-01-01
      • 2018-11-03
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多