Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have 2 tables in SQL , i use KnexJS to inset data.

I use "batchInsert" utility to insert object to one table.

my question:

Let say that my tables is :

  1. Product
  2. Product_meta

Can i insert this object to both of tables in on KnexJS call:

let products =
[{
    SKU: 'A',
    title: 'product A',
    product_meta: [
        { key: '_price', value: 20 },
        { key: '_qty', value: 10 }
    ]

}, {
    SKU: 'B',
    title: 'product B',
    product_meta: [
        { key: '_price', value: 9 },
        { key: '_qty', value: 50 }
    ]

}];

thanks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
416 views
Welcome To Ask or Share your Answers For Others

1 Answer

No. You need to do a separate insert for both of the tables. Knex doesn't know anything about SQL relations.

With objection.js (which is based of knex) it can be done if the relation mappings are declared correctly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...