Reputation: 4102
I have two entities: project
and actor
.
export abstract class BaseEntityModel {
@PrimaryGeneratedColumn()
id: number;
}
@Entity()
export class Project extends BaseEntityModel {
@Column()
title: string;
@OneToMany(type => Actor, actor => actor.project)
@JoinColumn()
actors: Actor[];
}
@Entity()
export class Actor extends BaseEntityModel {
@Column({unique: true})
title: string;
@ManyToOne(type => Project, project => project.actors)
project: Project;
}
I want unique actor title per project, but not across database.
projects: [
{
title: "webapp",
actors: [
{
title: "dev"
}
]
},
{
title: "json-parser",
actors: [
{
title: "dev"
},
{
title: "target"
}
]
}
]
projects: [
{
title: "webapp",
actors: [
{
title: "dev"
},
{
title: "dev"
}
]
},
{
title: "json-parser",
actors: [
{
title: "dev"
},
{
title: "target"
}
]
}
]
@Column({unique: true})
work as unique across table, that is not what I want. There is also class decorator @Unique(['title'])
, but it seems to be doing the same.
Is there something from TypeORM that I can use, or do I have to check myself if the actor.title
already exist in project. To avoid duplicated actor.title
in the same project. While keeping the possibility to have multiple time the same actor.title
across the database?
Note that Actor
entity should not be shared across projects
(no many to many relationship).
Upvotes: 3
Views: 3801
Reputation: 4102
Adding @Unique(['title', 'project'])
class decorator to Actor
seems to be what I was looking for, note that I had "corrupted" my database somehow and migration worked correctly only after deleting/creating it.
Upvotes: 7