One-To-Many relation issue
See original GitHub issueHi there, First of all, this is an awesome library 😃
The problem that I am facing now is that I am not getting the one-to-many relation data correctly and I know that I must be doing something wrong here, can some point out what I am doing wrong here. I have a Content table which have a many-to-many relation with ContentMusic via a link table music_in_content, also Content has a one-to-many relation with ContentImage table but I am not able to fetch the data for this ContentImage relation.
Below is my schema diagram:
Below is my code:
export class ContentMusic extends Model {
public readonly id: number;
public musicType: "express"|"spectral"|"space";
public musicFileUrl: string;
public contents: Content[];
public static tableName = "content_music";
public static relationMappings: RelationMappings = {
contents: {
relation: Model.ManyToManyRelation,
modelClass: join(__dirname, "Content"),
join: {
from: "content_music.id",
through: {
from: "music_in_content.content_music_id",
to: "music_in_content.content_id",
},
to: "content.id",
},
},
}
}
export class ContentImage extends Model {
public readonly id: number;
public contentId: number;
public contentRefId: number;
public image: string;
public content?: Content;
public static tableName = "content_images";
public static relationMappings: RelationMappings = {
content: {
relation: Model.BelongsToOneRelation,
modelClass: join(__dirname, "Content"),
join: {
from: "content_images.content_id",
to: "content.id",
},
},
}
}
export class Content extends Model {
public readonly id: number;
public category: "fiveSteps"|"music"|"empowerment"|"advance";
public refId: number;
public title: string;
public content: string;
public infoMessage: string;
public music?: ContentMusic[];
public images?: ContentImage[];
public static tableName = "content";
public static modelPaths = [__dirname]
public static relationMappings: RelationMappings = {
music: {
relation: Model.ManyToManyRelation,
modelClass: "ContentMusic",
join: {
from: "content.id",
through: {
from: "music_in_content.content_id",
to: "music_in_content.content_music_id",
},
to: "content_music.id",
},
},
images: {
relation: Model.HasManyRelation,
modelClass: "ContentImage",
join: {
from: "content.id",
to: "content_images.content_id",
},
},
}
}
The query is as follows:
const content = await Content.query()
.eager("[images, music]")
.where("category", "=", category).debug();
return content;
The result is coming out as follows:
I have scanned the documentation and couldn’t figure out the problem. The music relation is loading fine as it a many-to-many relationship.
I am using the latest version of the library and peer dependency
Any help would be highly appreciated. Thanks in advance 😃
Issue Analytics
- State:
- Created 4 years ago
- Comments:7
You are using
knexSnakeCaseMappers
and therefore need to define everything incamelCase
instead ofsnake_case
as explained in the docs. So for example instead of the current mappings, you need to write:How I do I implement one-to-one, one-to-many and many-to-many differently in objectionjs please