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)
typescript typeorm
add a comment |
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)
typescript typeorm
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
add a comment |
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)
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 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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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 at 8:56
Steve Ruben
19418
19418
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.
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.
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 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