Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

upsertGraph update rows based on different column than 'id'

I have a products table with id and supplier_id columns.

How do I make upsertGraph update rows based on supplier_id instead of id when I fetch products from supplier?

Now upsertGraph only uses insert because there is no id property present.

EDIT

I fetch product lists from several different suppliers containing id, name, price

I store products in my database with id, name, price and supplier_id

Therefor I'm trying to make upsertGraph update/insert based on supplier_id, to update products based on supplier_id instead of id.

Is this possible?

like image 590
J. Doe Avatar asked Nov 23 '25 13:11

J. Doe


2 Answers

Nope.

Well what you want is not possible with upsertGraph as the official docs states

By default upsertGraph method updates the objects that have an id, inserts objects that don't have an id and deletes all objects that are not present. This functionality can be modified in many ways by providing UpsertGraphOptions object as the second argument.

REF: https://vincit.github.io/objection.js/guide/query-examples.html#graph-upserts

Its mentioned that default functionality can be modified but modifying the search based on different column than id is not possible.

like image 57
Prakash Thete Avatar answered Nov 26 '25 03:11

Prakash Thete


Didnt try before but I think you could use idColumn:

class Supplier extends Model {
  static get idColumn() {
    return 'supplier_id';
  }
}
like image 33
Rashomon Avatar answered Nov 26 '25 02:11

Rashomon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!