question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

One-To-Many relation issue

See original GitHub issue

Hi 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:

image

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:

image

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 image

Any help would be highly appreciated. Thanks in advance 😃

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
koskimascommented, Feb 18, 2020

You are using knexSnakeCaseMappers and therefore need to define everything in camelCase instead of snake_case as explained in the docs. So for example instead of the current mappings, you need to write:

    public static relationMappings: RelationMappings = {
        music: {
            relation: Model.ManyToManyRelation,
            modelClass: "ContentMusic",
            join: {
                from: "content.id",
                through: {
                    from: "musicInContent.contentId",
                    to: "musicInContent.contentMusicId",
                },
                to: "contentMusic.id",
            },
        },
        images: {
            relation: Model.HasManyRelation,
            modelClass: "ContentImage",
            join: {
                from: "content.id",
                to: "contentImages.contentId",
            },
        },
    }
0reactions
neriosoftcommented, Aug 16, 2022

How I do I implement one-to-one, one-to-many and many-to-many differently in objectionjs please

Read more comments on GitHub >

github_iconTop Results From Across the Web

The best way to map a @OneToMany relationship with JPA ...
The @ManyToOne annotation allows you to map the Foreign Key column in the child entity mapping so that the child has an entity...
Read more >
OneToMany relationship with JPA and Hibernate
One to many mapping with join table. This problem can be solved in two different ways. One is to have a foreign key...
Read more >
Best Practices for Many-To-One and ...
The definition of an unidirectional one-to-many association doesn't seem to be an issue. You just need an attribute that maps the association and...
Read more >
OneToMany relationship issues - symfony
I have two entities, PartenairePermission and StructurePermission , I'm trying to get properties from the other entity in a One To Many ......
Read more >
One-to-many relationships
In a one-to-many relationship, one record in a table can be associated with one or more records in another table. For example, each...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found