2016-06-13 122 views
1

我有一個數組批插入到MySQL數據庫

newData = [{ sId: 'XXXXXX', itemlName: 'LSストレッ', iCode: 'XXXXXX', iType: '', skus: 'XXXXXXX', iLevel: 'L2', cCode: '88', cName: 'Other', sCode: '999', sName: 'No Control', pLengthCode: '988', core: 'N', sCode: '1', dCode: 'XX', gDeptCode: 'XX', gDeptName: 'Women\\\'s Items', rCode: 'jqs' },{ sId: 'XXXXXX', itemlName: 'LSストレッ', iCode: 'XXXXXX', iType: '', skus: 'XXXXXXX', iLevel: 'L2', cCode: '88', cName: 'Other', sCode: '999', sName: 'No Control', pLengthCode: '988', core: 'N', sCode: '1', dCode: 'XX', gDeptCode: 'XX', gDeptName: 'Women\\\'s Items', rCode: 'jqs' }] 

我希望使用objection.js中插入newData到MySQL數據庫。但是,當我跑我的節點應用,我得到了錯誤說:

Error: batch insert only works with Postgresql

我的插入代碼的 - :

samplemodel.query().insert(newData); 

我如何使用反對在MySQL數據庫中執行數組數據的批量插入.js文件?

回答

1

錯誤:批量插入只能從PostgreSQL

好工作,這意味着你需要的Postgres的Mysql的,而不是使它工作。

EDIT1:

從文檔:

If you are using Postgres the inserts are done in batches for maximum performance. On other databases the rows need to be inserted one at a time. This is because postgresql is the only database engine that returns the identifiers of all inserted rows and not just the first or the last one.

這意味着,如果你正在使用MySQL,你應該做的

samplemodel.query().insert(data); 

對於 「newData」 數組中的每個元素。

1

您可以撥打

samplemodel.query().insertWithRelated(newData);

如果需要的話,它使用多個查詢,並與所有數據庫工作。