-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Closed
Description
Issue type:
[X] question
[?] bug report
Database system/driver:
[X] mysql / mariadb
TypeORM version:
[X] 0.2.6 (or put your version here)
Steps to reproduce or a small repository showing the problem:
My entities are like this
@Entity('User')
class User {
@PrimaryColumn('varchar', { length: 120 })
public id: string;
@Column('varchar', { length: 120 })
public name: string;
@OneToMany(() => Comment, (comment: Comment) => comment.user, {
cascade: true,
eager: true
})
public comments: Comment[];
}
@Entity('Comment')
class Comment {
@PrimaryGeneratedColumn('uuid')
public id: string;
@Column('varchar', { length: 500 })
public content: string;
@ManyToOne(() => User, (user: User) => user.comments, {
onDelete: 'CASCADE'
})
@JoinColumn({ name: 'userId' })
public comment: Comment;
@Column('datetime', { nullable: true })
public deletedAt: Date | null;
}
** I guess it could have some typos or mistakes because it is just code sample for describing my settings
My problem is
- I am working with DDD, so
Useris an aggregate which containsComments - In
Useraggregate,Useris aggregate root and entity, comment is just value object. - In DDD, VO must be replaced new value object when it should be modified.
- So, my job is here. I want to modify user's comment, in this time, i should create new VO as following it's change and save it. Including deletion of original comment. But I can't do that, because original comments are not deleted, just have
nullin it's foreign key. I know that I didn't setnullable=false, but If I set this option to false, an error occurs:QueryFailedError: ER_BAD_NULL_ERROR: Column 'UserId' cannot be null.
** Of course TypeOrm entities will be mapped to Domain Objects.
May they be like this:
class User {
private userId: number;
private comments: Comment[]
...
public modifyComment(origin: Comment) {
comments.push(new Comment('blah blah'));
this.removeComment(origin);
}
}
- I can handle it modifying orignal object instead of creating new one and deleting it, however, it is not what I want to do. I am trying to strictly follow rules of DDD.
What I want is
- How can I remove an
commentclearly instead of insertingnullvalue to it's foreign keyuserId? - and Why
nullable=falseis not working as I expected?
Thank you for your supporting.
feather-jmalone, darky, Nosfistis, doocaat, kordeviant and 1 more