typeorm: saving an entity with many-to-many relationship ids
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
add a comment |
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
1
I think you should create the usergroup first, and add the created usergroup to project.
– Steve Ruben
Nov 22 '18 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 '18 at 8:42
1
yes, you can do like that.
– Steve Ruben
Nov 22 '18 at 8:49
I see. Thanks! Would be better if we could simply pass ids tho...
– sora
Nov 22 '18 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 '18 at 8:57
add a comment |
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
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
typescript typeorm
asked Nov 22 '18 at 8:31
sorasora
212311
212311
1
I think you should create the usergroup first, and add the created usergroup to project.
– Steve Ruben
Nov 22 '18 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 '18 at 8:42
1
yes, you can do like that.
– Steve Ruben
Nov 22 '18 at 8:49
I see. Thanks! Would be better if we could simply pass ids tho...
– sora
Nov 22 '18 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 '18 at 8:57
add a comment |
1
I think you should create the usergroup first, and add the created usergroup to project.
– Steve Ruben
Nov 22 '18 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 '18 at 8:42
1
yes, you can do like that.
– Steve Ruben
Nov 22 '18 at 8:49
I see. Thanks! Would be better if we could simply pass ids tho...
– sora
Nov 22 '18 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 '18 at 8:57
1
1
I think you should create the usergroup first, and add the created usergroup to project.
– Steve Ruben
Nov 22 '18 at 8:35
I think you should create the usergroup first, and add the created usergroup to project.
– Steve Ruben
Nov 22 '18 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 '18 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 '18 at 8:42
1
1
yes, you can do like that.
– Steve Ruben
Nov 22 '18 at 8:49
yes, you can do like that.
– Steve Ruben
Nov 22 '18 at 8:49
I see. Thanks! Would be better if we could simply pass ids tho...
– sora
Nov 22 '18 at 8:53
I see. Thanks! Would be better if we could simply pass ids tho...
– sora
Nov 22 '18 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 '18 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 '18 at 8:57
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Nov 22 '18 at 8:56
Steve RubenSteve Ruben
322410
322410
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
I think you should create the usergroup first, and add the created usergroup to project.
– Steve Ruben
Nov 22 '18 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 '18 at 8:42
1
yes, you can do like that.
– Steve Ruben
Nov 22 '18 at 8:49
I see. Thanks! Would be better if we could simply pass ids tho...
– sora
Nov 22 '18 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 '18 at 8:57