未定义 ObjectionJS 关系模型类
ObjectionJS relationship modelClass is not defined
我正在尝试更新 user
和 user_profile
之间的关系。我可以更新用户,但是当我尝试更新 user_profile
:
时,我一直收到此错误
ERROR: Error: UserProfile.relationMappings.user: modelClass is not defined
据我所知,我已经模仿了文档中的示例和 TypeScript 示例,但是有人能看出为什么这不起作用吗?
我包括了查询、两个模型和用户配置文件迁移。 BaseModel的内容被注释掉了,相当于直接继承了Model,jsonSchema
只是为了验证,为了简洁我去掉了。
更新
从 UserProfile
中删除 relationshipMappings
可阻止错误发生,但由于我需要 BelongsToOneRelation
关系,我仍在尝试。至少它似乎缩小到 UserProfile
.
上的 relationMappings
查询
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 });
型号
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'
}
}
};
}
迁移
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);
});
};
我会说
a) 是循环依赖的问题or/and
b) 导入路径有问题
一种是在 modelClass 中使用绝对文件路径而不是构造函数。例如
modelClass: __dirname + '/User'
或
modelClass: require('./User').default
查看示例:
https://github.com/Vincit/objection.js/blob/master/examples/express-es7/src/models/Animal.js
我正在尝试更新 user
和 user_profile
之间的关系。我可以更新用户,但是当我尝试更新 user_profile
:
ERROR: Error: UserProfile.relationMappings.user: modelClass is not defined
据我所知,我已经模仿了文档中的示例和 TypeScript 示例,但是有人能看出为什么这不起作用吗?
我包括了查询、两个模型和用户配置文件迁移。 BaseModel的内容被注释掉了,相当于直接继承了Model,jsonSchema
只是为了验证,为了简洁我去掉了。
更新
从 UserProfile
中删除 relationshipMappings
可阻止错误发生,但由于我需要 BelongsToOneRelation
关系,我仍在尝试。至少它似乎缩小到 UserProfile
.
relationMappings
查询
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 });
型号
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'
}
}
};
}
迁移
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);
});
};
我会说
a) 是循环依赖的问题or/and b) 导入路径有问题
一种是在 modelClass 中使用绝对文件路径而不是构造函数。例如
modelClass: __dirname + '/User'
或
modelClass: require('./User').default
查看示例: https://github.com/Vincit/objection.js/blob/master/examples/express-es7/src/models/Animal.js