在 Objection.js 中插入多对多的额外字段
Insert with extra field with in many-to-many in Objection.js
我有以下疑问,我在 objection.js 文档中找不到。
我有以下 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 }
]
}
如您所见,我不想创建新语言,我只想添加带有额外 属性 的引用。我只是想建立与国家的关系。
这样插入的正确方法是什么?我找不到文档,只是找到了一种创建语言的方法。
谢谢!
我问过项目维护者,所以我会用他的回答来回答我自己的问题,以防其他人感兴趣:
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
}]
})
现在你应该可以简单地说:
Country.query()
.insertGraph({
name: "Country Name",
languages: [
{ id: 1, official: true },
{ id: 2, official: true },
{ id: 3, official: false }
]
}, {
// This tells `insertGraph` to not insert new languages.
relate: true
});
我有以下疑问,我在 objection.js 文档中找不到。 我有以下 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 }
]
}
如您所见,我不想创建新语言,我只想添加带有额外 属性 的引用。我只是想建立与国家的关系。 这样插入的正确方法是什么?我找不到文档,只是找到了一种创建语言的方法。
谢谢!
我问过项目维护者,所以我会用他的回答来回答我自己的问题,以防其他人感兴趣:
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
}]
})
现在你应该可以简单地说:
Country.query()
.insertGraph({
name: "Country Name",
languages: [
{ id: 1, official: true },
{ id: 2, official: true },
{ id: 3, official: false }
]
}, {
// This tells `insertGraph` to not insert new languages.
relate: true
});