英文:
Supabase- check if a Postgres JSON array contains a number
问题
I have a jsonb column named 'units' that contains data like: {"units": [123, 220]}
I want to select the row if any number in the array matches the unit parameter
async fetchAccount(unit: number) {
  try {
    let { data, error } = await this.supabase
      .from('accounts')
      .select('*')
      .eq('units:jsonb->units', '[' + unit + ']').single();  
  } catch (error) {   
  }
}
Returns 0 rows. I tried 'contains' rather than eq with the same result.
英文:
I have a jsonb column named 'units' that contains data like: {"units": [123,220]}
I want to select the row if any number in the array matches the unit parameter
async fetchAccount(unit: number) {
  try {
    let { data, error } = await this.supabase
      .from('accounts')
      .select('*')
      .eq('units:jsonb->units', '[' + unit +']').single();  
  } catch (error) {   
  }
}
Returns 0 rows. I tried 'contains' rather than eq with same result.
答案1
得分: 1
"Conatins should work for this" should be translated as "这里应该适用 contains:"
"units->units to access the units elements within the units column, and [unit] to find arrays that include all items in the array." should be translated as "units->units 用于访问 units 列中的 units 元素,而 [unit] 用于查找包含数组中所有项的数组。"
英文:
Conatins should work for this:
async fetchAccount(unit: number) {
  try {
    let { data, error } = await this.supabase
      .from('accounts')
      .select('*')
      .contains('units->units', [unit])
      .single();  
  } catch (error) {   
  }
}
units->units to access the units elements within the units column, and [unit] to find arrays that include all items in the array.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论