I'm trying to relate two models with an extra property of "url":
if (typeof session.id === "number") {
const sessionUser = await Session.relatedQuery("users")
.for(session.id)
.relate({
id: 12345, // error here
url: "test",
});
}
The error I get from typescript is this:
Argument of type '{ id: number; url: string; }' is not assignable to parameter of type 'string | number | CompositeId | MaybeCompositeId[] | PartialModelObject | PartialModelObject[]'.
Object literal may only specify known properties, and 'id' does not exist in type 'CompositeId | MaybeCompositeId[] | PartialModelObject | PartialModelObject[]'.
However, if I try to relate the models without the extra property, it works perfectly:
if (typeof session.id === "number") {
const sessionUser = await Session.relatedQuery("users")
.for(session.id)
.relate(userId); //userId is just a number e.g. 5
}
Here's my Session model. I also have the extra property on my User model too, as a safety net:
export class Session extends Model {
host_id?: number;
url?: string;
static get tableName() {
return "sessions";
}
static relationMappings = {
users: {
relation: Model.ManyToManyRelation,
modelClass: path.join(__dirname, "User"),
join: {
from: "sessions.id",
through: {
from: "sessions_users.session_id",
to: "sessions_users.user_id",
extra: ["url"],
},
to: "users.id",
},
},
};
}
Here are the docs for extra properties: https://vincit.github.io/objection.js/recipes/extra-properties.html
Am I missing anything obvious here?
question from:
https://stackoverflow.com/questions/65938915/cant-relate-model-with-an-extra-property-in-objection-due-to-typescript-error 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…