typeorm: saving an entity with many-to-many relationship ids











up vote
0
down vote

favorite












Is it possible to save an entity with many-to-many relation ids?



suppose I have following Project Entity with many-to-many relationship to userGroups table.



@Entity()
export class Project extends BaseEntity {
@Column({ type: 'varchar', length: 255 })
name: string

@RelationId((project: Project) => project.userGroups)
userGroupIds: number

@ManyToMany(type => UserGroup, userGroup => userGroup.projects)
@JoinTable()
userGroups: UserGroup
}


Since ids of the userGroups table are mapped to userGroupIds property of the Project class via @RelationId decorator, I thought I could save a new Project entity with userGroupIds like this:



let prj = new Project()
prj.name = 'foo'
prj.userGroupIds = [1, 2, 3]
prj.save()


but the above code only creates a project record... (no record is created on project - userGroups many-to-many relation table)










share|improve this question


















  • 1




    I think you should create the usergroup first, and add the created usergroup to project.
    – Steve Ruben
    Nov 22 at 8:35










  • Thank you for your comment. You mean if i have 3 existing records(id=1,2,3) on user groups table, we need to create 3 instances of UserGroup entities from those records and add them to project entity?
    – sora
    Nov 22 at 8:42








  • 1




    yes, you can do like that.
    – Steve Ruben
    Nov 22 at 8:49










  • I see. Thanks! Would be better if we could simply pass ids tho...
    – sora
    Nov 22 at 8:53










  • nope, because you don't always know the id, in test yes but in production you don't know
    – Steve Ruben
    Nov 22 at 8:57















up vote
0
down vote

favorite












Is it possible to save an entity with many-to-many relation ids?



suppose I have following Project Entity with many-to-many relationship to userGroups table.



@Entity()
export class Project extends BaseEntity {
@Column({ type: 'varchar', length: 255 })
name: string

@RelationId((project: Project) => project.userGroups)
userGroupIds: number

@ManyToMany(type => UserGroup, userGroup => userGroup.projects)
@JoinTable()
userGroups: UserGroup
}


Since ids of the userGroups table are mapped to userGroupIds property of the Project class via @RelationId decorator, I thought I could save a new Project entity with userGroupIds like this:



let prj = new Project()
prj.name = 'foo'
prj.userGroupIds = [1, 2, 3]
prj.save()


but the above code only creates a project record... (no record is created on project - userGroups many-to-many relation table)










share|improve this question


















  • 1




    I think you should create the usergroup first, and add the created usergroup to project.
    – Steve Ruben
    Nov 22 at 8:35










  • Thank you for your comment. You mean if i have 3 existing records(id=1,2,3) on user groups table, we need to create 3 instances of UserGroup entities from those records and add them to project entity?
    – sora
    Nov 22 at 8:42








  • 1




    yes, you can do like that.
    – Steve Ruben
    Nov 22 at 8:49










  • I see. Thanks! Would be better if we could simply pass ids tho...
    – sora
    Nov 22 at 8:53










  • nope, because you don't always know the id, in test yes but in production you don't know
    – Steve Ruben
    Nov 22 at 8:57













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Is it possible to save an entity with many-to-many relation ids?



suppose I have following Project Entity with many-to-many relationship to userGroups table.



@Entity()
export class Project extends BaseEntity {
@Column({ type: 'varchar', length: 255 })
name: string

@RelationId((project: Project) => project.userGroups)
userGroupIds: number

@ManyToMany(type => UserGroup, userGroup => userGroup.projects)
@JoinTable()
userGroups: UserGroup
}


Since ids of the userGroups table are mapped to userGroupIds property of the Project class via @RelationId decorator, I thought I could save a new Project entity with userGroupIds like this:



let prj = new Project()
prj.name = 'foo'
prj.userGroupIds = [1, 2, 3]
prj.save()


but the above code only creates a project record... (no record is created on project - userGroups many-to-many relation table)










share|improve this question













Is it possible to save an entity with many-to-many relation ids?



suppose I have following Project Entity with many-to-many relationship to userGroups table.



@Entity()
export class Project extends BaseEntity {
@Column({ type: 'varchar', length: 255 })
name: string

@RelationId((project: Project) => project.userGroups)
userGroupIds: number

@ManyToMany(type => UserGroup, userGroup => userGroup.projects)
@JoinTable()
userGroups: UserGroup
}


Since ids of the userGroups table are mapped to userGroupIds property of the Project class via @RelationId decorator, I thought I could save a new Project entity with userGroupIds like this:



let prj = new Project()
prj.name = 'foo'
prj.userGroupIds = [1, 2, 3]
prj.save()


but the above code only creates a project record... (no record is created on project - userGroups many-to-many relation table)







typescript typeorm






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 8:31









sora

192111




192111








  • 1




    I think you should create the usergroup first, and add the created usergroup to project.
    – Steve Ruben
    Nov 22 at 8:35










  • Thank you for your comment. You mean if i have 3 existing records(id=1,2,3) on user groups table, we need to create 3 instances of UserGroup entities from those records and add them to project entity?
    – sora
    Nov 22 at 8:42








  • 1




    yes, you can do like that.
    – Steve Ruben
    Nov 22 at 8:49










  • I see. Thanks! Would be better if we could simply pass ids tho...
    – sora
    Nov 22 at 8:53










  • nope, because you don't always know the id, in test yes but in production you don't know
    – Steve Ruben
    Nov 22 at 8:57














  • 1




    I think you should create the usergroup first, and add the created usergroup to project.
    – Steve Ruben
    Nov 22 at 8:35










  • Thank you for your comment. You mean if i have 3 existing records(id=1,2,3) on user groups table, we need to create 3 instances of UserGroup entities from those records and add them to project entity?
    – sora
    Nov 22 at 8:42








  • 1




    yes, you can do like that.
    – Steve Ruben
    Nov 22 at 8:49










  • I see. Thanks! Would be better if we could simply pass ids tho...
    – sora
    Nov 22 at 8:53










  • nope, because you don't always know the id, in test yes but in production you don't know
    – Steve Ruben
    Nov 22 at 8:57








1




1




I think you should create the usergroup first, and add the created usergroup to project.
– Steve Ruben
Nov 22 at 8:35




I think you should create the usergroup first, and add the created usergroup to project.
– Steve Ruben
Nov 22 at 8:35












Thank you for your comment. You mean if i have 3 existing records(id=1,2,3) on user groups table, we need to create 3 instances of UserGroup entities from those records and add them to project entity?
– sora
Nov 22 at 8:42






Thank you for your comment. You mean if i have 3 existing records(id=1,2,3) on user groups table, we need to create 3 instances of UserGroup entities from those records and add them to project entity?
– sora
Nov 22 at 8:42






1




1




yes, you can do like that.
– Steve Ruben
Nov 22 at 8:49




yes, you can do like that.
– Steve Ruben
Nov 22 at 8:49












I see. Thanks! Would be better if we could simply pass ids tho...
– sora
Nov 22 at 8:53




I see. Thanks! Would be better if we could simply pass ids tho...
– sora
Nov 22 at 8:53












nope, because you don't always know the id, in test yes but in production you don't know
– Steve Ruben
Nov 22 at 8:57




nope, because you don't always know the id, in test yes but in production you don't know
– Steve Ruben
Nov 22 at 8:57












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










suppose we



@Entity()
export class Project extends BaseEntity {
@Column({ type: 'varchar', length: 255 })
name: string

@RelationId((project: Project) => project.userGroups)
userGroupIds: number

@ManyToMany(type => UserGroup, userGroup => userGroup.projects)
@JoinTable()
userGroups: UserGroup
}


to persist, i can



let prj = new Project()
prj.name = 'foo'
userGroup a = // create a user group here
prj.userGroupIds.add(a)
prj.save()

after persist, you will see a record in the table project - userGroups
|project| |projet-usergroup| |usergroup|
--------- ------------------ -----------
|id | |idproj|idusergrp| |idusergrp|

|1 | |1 | 1 | | 1 |

after all you will have something like that





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426715%2ftypeorm-saving-an-entity-with-many-to-many-relationship-ids%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    suppose we



    @Entity()
    export class Project extends BaseEntity {
    @Column({ type: 'varchar', length: 255 })
    name: string

    @RelationId((project: Project) => project.userGroups)
    userGroupIds: number

    @ManyToMany(type => UserGroup, userGroup => userGroup.projects)
    @JoinTable()
    userGroups: UserGroup
    }


    to persist, i can



    let prj = new Project()
    prj.name = 'foo'
    userGroup a = // create a user group here
    prj.userGroupIds.add(a)
    prj.save()

    after persist, you will see a record in the table project - userGroups
    |project| |projet-usergroup| |usergroup|
    --------- ------------------ -----------
    |id | |idproj|idusergrp| |idusergrp|

    |1 | |1 | 1 | | 1 |

    after all you will have something like that





    share|improve this answer

























      up vote
      1
      down vote



      accepted










      suppose we



      @Entity()
      export class Project extends BaseEntity {
      @Column({ type: 'varchar', length: 255 })
      name: string

      @RelationId((project: Project) => project.userGroups)
      userGroupIds: number

      @ManyToMany(type => UserGroup, userGroup => userGroup.projects)
      @JoinTable()
      userGroups: UserGroup
      }


      to persist, i can



      let prj = new Project()
      prj.name = 'foo'
      userGroup a = // create a user group here
      prj.userGroupIds.add(a)
      prj.save()

      after persist, you will see a record in the table project - userGroups
      |project| |projet-usergroup| |usergroup|
      --------- ------------------ -----------
      |id | |idproj|idusergrp| |idusergrp|

      |1 | |1 | 1 | | 1 |

      after all you will have something like that





      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        suppose we



        @Entity()
        export class Project extends BaseEntity {
        @Column({ type: 'varchar', length: 255 })
        name: string

        @RelationId((project: Project) => project.userGroups)
        userGroupIds: number

        @ManyToMany(type => UserGroup, userGroup => userGroup.projects)
        @JoinTable()
        userGroups: UserGroup
        }


        to persist, i can



        let prj = new Project()
        prj.name = 'foo'
        userGroup a = // create a user group here
        prj.userGroupIds.add(a)
        prj.save()

        after persist, you will see a record in the table project - userGroups
        |project| |projet-usergroup| |usergroup|
        --------- ------------------ -----------
        |id | |idproj|idusergrp| |idusergrp|

        |1 | |1 | 1 | | 1 |

        after all you will have something like that





        share|improve this answer












        suppose we



        @Entity()
        export class Project extends BaseEntity {
        @Column({ type: 'varchar', length: 255 })
        name: string

        @RelationId((project: Project) => project.userGroups)
        userGroupIds: number

        @ManyToMany(type => UserGroup, userGroup => userGroup.projects)
        @JoinTable()
        userGroups: UserGroup
        }


        to persist, i can



        let prj = new Project()
        prj.name = 'foo'
        userGroup a = // create a user group here
        prj.userGroupIds.add(a)
        prj.save()

        after persist, you will see a record in the table project - userGroups
        |project| |projet-usergroup| |usergroup|
        --------- ------------------ -----------
        |id | |idproj|idusergrp| |idusergrp|

        |1 | |1 | 1 | | 1 |

        after all you will have something like that






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 8:56









        Steve Ruben

        19418




        19418






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426715%2ftypeorm-saving-an-entity-with-many-to-many-relationship-ids%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)