-
Notifications
You must be signed in to change notification settings - Fork 643
Closed
Description
Includes models and code that has been executed that shows the issue.
Models:
import { Model, snakeCaseMappers, JSONSchema } from 'objection';
import { UserRole } from './UserRole';
export class User extends Model {
public static tableName = 'users';
public static columnNameMappers = snakeCaseMappers();
public static virtualAttributes = ['fullName'];
public static jsonSchema: JSONSchema = {
type: 'object',
required: ['email', 'firstName', 'lastName'],
properties: {
id: { type: 'string' },
email: { type: 'string', minLength: 1, maxLength: 255 },
firstName: { type: 'string', minLength: 1, maxLength: 255 },
lastName: { type: 'string', minLength: 1, maxLength: 255 },
},
};
public static relationMappings = {
roles: {
relation: Model.HasManyRelation,
modelClass: UserRole,
join: {
from: 'users.id',
to: 'user_roles.user_id',
},
},
};
public id: string;
public email: string;
public firstName: string;
public lastName: string;
public roles: UserRole[];
get fullName(): string {
return `${this.firstName} ${this.lastName}`;
}
}
import { Model, snakeCaseMappers, JSONSchema } from 'objection';
export class UserRole extends Model {
public static tableName = 'user_roles';
public static columnNameMappers = snakeCaseMappers();
public static jsonSchema: JSONSchema = {
type: 'object',
required: ['userId', 'projectId'],
properties: {
id: { type: 'string' },
userId: {},
projectId: {},
role: { type: 'string' },
},
};
public id: string;
public userId: string;
public projectId: string;
public role: string;
}
The reason I get the users to start is that the ids are stored as varbinary(16)
Code using eager that does work in Objection 1 and not Objection 2:
const users = await User.query();
const user = await User.query()
.where('id', users[0].id)
.eager('roles');
console.log(user[0].roles);
Code using withGraphJoined had to add fully qualifying table name
const users = await User.query();
const user = await User.query()
.where('users.id', users[0].id)
.withGraphJoined('roles');
console.log(user[0].roles);
Using: 2.0.7
- Trying: Model query with a where clause and joining another table using eager through relationMappings
- Expected: Work the same as Objection 1
- Actual: Returned data without roles
- Diff: The binding does not stay the same
Note: Worked around issue by using withGraphJoined and fully qualifying queries with tablename instead of using withGraphFetched or eager
Objection 1:
{
method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [ <Buffer 75 de d3 01 82 72 4e 1e 8a 25 4e f9 35 19 3f 76> ],
__knexQueryUid: 'e6506304-2026-4001-a869-0c120c269042',
sql: 'select `users`.* from `users` where `users`.`id` = ?'
}
{
method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [ <Buffer 75 de d3 01 82 72 4e 1e 8a 25 4e f9 35 19 3f 76> ],
__knexQueryUid: '253629db-a95b-4525-8839-b48924fbdd23',
sql: 'select `user_roles`.* from `user_roles` where `user_roles`.`user_id` in (?)'
}
Objection 2:
{
method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [ <Buffer 75 de d3 01 82 72 4e 1e 8a 25 4e f9 35 19 3f 76> ],
__knexQueryUid: 'd6880e55-139f-4f7d-8704-9b2006f5a381',
sql: 'select `users`.* from `users` where `users`.`id` = ?'
}
{
method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [ 'u��\u0001�rN\u001e�%N�5\u0019?v' ],
__knexQueryUid: 'ac1da74c-7b21-4352-9c76-ffd81a351ef6',
sql: 'select `user_roles`.* from `user_roles` where `user_roles`.`user_id` in (?)'
}
Metadata
Metadata
Assignees
Labels
No labels