I'm trying to update a relationship between a user
and user_profile
. I can update the user, but I keep getting this error when I try to update the user_profile
:
ERROR: Error: UserProfile.relationMappings.user: modelClass is not defined
As far as I can tell I've mimicked the example from the docs, and TypeScript example, but can anyone see why this isn't working?
I included the query, both models, and the user profile migration. The contents of the BaseModel are commented out so it is equivalent to inheriting from Model directly, and the jsonSchema
is only for validation so I removed them for brevity.
UPDATE
Removing the relationshipMappings
from UserProfile
stops the error from occurring, but since I need the BelongsToOneRelation
relationship I'm still trying. At least it seems to be narrowed down to the relationMappings
on the UserProfile
.
Query
const user = await User.query() // <--- Inserts the user
.insert({ username, password });
const profile = await UserProfile.query() <--- Throws error
.insert({ user_id: 1, first_name, last_name });
Models
import { Model, RelationMappings } from 'objection';
import { BaseModel } from './base.model';
import { UserProfile } from './user-profile.model';
export class User extends BaseModel {
readonly id: number;
username: string;
password: string;
role: string;
static tableName = 'users';
static jsonSchema = { ... };
static relationMappings: RelationMappings = {
profile: {
relation: Model.HasOneRelation,
modelClass: UserProfile,
join: {
from: 'users.id',
to: 'user_profiles.user_id'
}
}
};
}
import { Model, RelationMappings } from 'objection';
import { BaseModel } from './base.model';
import { User } from './user.model';
export class UserProfile extends BaseModel {
readonly id: number;
user_id: number;
first_name: string;
last_name: string;
static tableName = 'user_profiles';
static jsonSchema = { ... };
static relationMappings: RelationMappings = {
user: {
relation: Model.BelongsToOneRelation,
modelClass: User,
join: {
from: 'user_profiles.user_id',
to: 'users.id'
}
}
};
}
Migration
exports.up = function (knex, Promise) {
return knex.schema
.createTable('user_profiles', (table) => {
table.increments('id').primary();
table.integer('user_id')
.unsigned()
.notNullable();
table.foreign('user_id')
.references('users.id');
table.string('first_name');
table.string('last_name');
table.timestamps(true, true);
});
};