How to use id in Query in BookshelfJS?

I have one field and it has a semicolon identifier , so I want to find from this selected identifier, here is my code,

.get(function(req, res) {
  knex.select('*')
  .from('exam')
  .whereRaw('? = any(regexp_split_to_array(student_id))', [req.params.id])
  .then(function(rows) {
    //return res.send(rows);
    console.log(rows);
  })
  .catch(function(error) {
    console.log(error)
  });
});

===> while I use KNEX , it will throw an error.

{ error: function regexp_split_to_array(text) does not exist
  name: 'error',
  length: 220,
  severity: 'ERROR',
  code: '42883',
  detail: undefined,
  hint: 'No function matches the given name and argument types. You might need to add explicit type casts.',
  position: '37',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'parse_func.c',
  line: '523',
  routine: 'ParseFuncOrColumn' 
}
  • in the student_id column i have an ID like this, 33,34,35,36
  • in req.params.id I have only one ID, for example 35.
  • so I want rows that include 35 identifiers in the same table.

enter image description here

===> So, I want only two rows (2,3), because it includes ID = 35.

+4
source share
1

, PostgreSQL ( , phpPgAdmin ). regexp_split_to_array (:). .

SQL :

select '35' = any(regexp_split_to_array('33,34,35,36', E','));

.where

.whereRaw("? = any(regexp_split_to_array(student_id, E','))", [req.params.id])

, , . ( , ) , student_id gin student_id , this

select * from table where student_id @> '{35}';
+1

Source: https://habr.com/ru/post/1695527/


All Articles