Skip to content

Cannot delete entities on 1:N relationship with nullable: false #2467

@imione

Description

@imione

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 User is an aggregate which contains Comments
  • In User aggregate, User is 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 null in it's foreign key. I know that I didn't set nullable=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 comment clearly instead of inserting null value to it's foreign key userId?
  • and Why nullable=false is not working as I expected?

Thank you for your supporting.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions