Diesel ORM: How to implement a generic delete function?

Hi,

I want to implement a generic delete function for deleting records from any table based on table's id of type i32.

for example

delere_records::<table_A>(32) // where 32 is the pk id of table A

delere_records::<table_B>(23) // where 23 is the pk id of table B

How to define this 'delete_records' function in generic way so that it can accept any sqlite table.

Please suggest.

I tried but don't know what should I write in where block:

pub fn delete_records<T>(&self, pk_id: i32)->bool
      where
         
   {
      let flag;
      let db_connection = self.pool.get().unwrap();
      let count = diesel::delete(T::table().filter(id.eq(pk_id))).execute(&db_connection).unwrap();
      if count > 0 {
         flag = true;
      } else {
         flag = false;
      }
      flag
   }

please suggest.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.