用Objection.js中的多对多插入

我有以下怀疑,我无法在objection.js文档中find清楚的答案。 我有以下2个模型:

export class Language extends BaseId { name: string; static tableName = 'Languages'; static jsonSchema = { type: 'object', required: ['name'], properties: { name: { type: 'string', minLength: 1, maxLength: 80 } } }; } export class Country extends BaseId { name: string; languages: Language[]; static tableName = 'Countries'; static jsonSchema = { type: 'object', required: ['name'], properties: { name: { type: 'string', minLength: 1, maxLength: 120 } } }; static relationMappings: RelationMappings = { languages: { relation: Model.ManyToManyRelation, modelClass: Language, join: { from: 'Countries.id', through: { from: 'CountriesLanguages.country_id', to: 'CountriesLanguages.language_id', extra: ['oficial'] }, to: 'Languages.id' } } }; } 

我想插入一个新的国家,如:

 { name: "Country Name", languages: [ { id: 1, official: true }, { id: 2, official: true }, { id: 3, official: false } ] } 

正如你所看到的,我不想创build一个新的语言,我只想添加引用与额外的属性。 我只想创build与关系的国家。 什么是这样插入的正确方法? 我无法find文档,只是find了创build语言的方法。

谢谢!

我向项目维护人员询问了这个问题,所以我会用他的回答回答我自己的问题,以防其他人感兴趣:

 Country .query() .insertGraph({ name: 'Finland', languages: [{ // This is the id of the language. You can change "#dbRef" into // some other property by overriding `Model.dbRefProp` but you // cannot use the model's id column name. "#dbRef": 1, oficial: true }, { "#dbRef": 2, oficial: false }] }) 

来自: https : //github.com/Vincit/objection.js/issues/441