【发布时间】:2017-07-12 14:48:13
【问题描述】:
我有以下疑问,我无法在 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 }
]
}
如您所见,我不想创建新语言,我只想添加具有额外属性的引用。我只想用这种关系创造国家。 这样插入的正确方法是什么?我找不到文档,只是找到了一种创建语言的方法。
谢谢!
【问题讨论】:
标签: javascript node.js typescript knex.js objection.js