Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not defiend ids in one-to-many relations #2977

Closed
username1103 opened this issue Mar 31, 2022 · 3 comments
Closed

Not defiend ids in one-to-many relations #2977

username1103 opened this issue Mar 31, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@username1103
Copy link

Describe the bug
Hi, First of all, I'm not goot at English.. sorry.

I'm found problem in one-to-many and many-to-one relations. If add one item in collection, then id is defined after call persist and flush. But if add mulit item in collection, then ids aren't defined after call persist and flush. this is my repo

Stack trace
My Entity

./book.entity.ts

@Entity()
export class Book {
  @PrimaryKey({ name: "book_id", autoincrement: true })
  private id: number;

  @Property()
  private name: string;

  @ManyToOne("Author", { name: "author_id", inversedBy: "books" })
  private author: IdentifiedReference<Author>;

  getId() {
    return this.id;
  }

  getName() {
    return this.name;
  }

  getAuthor() {
    return this.author;
  }

  setName(name: string) {
    this.name = name;
  }

  setAuthor(author: Author) {
    this.author = wrap(author).toReference();
  }
}
./author.entity.ts


@Entity()
export class Author {
  @PrimaryKey({ name: "author_id", autoincrement: true })
  private id!: number;

  @Property()
  private name!: string;

  @OneToMany("Book", "author")
  private books = new Collection<Book>(this);

  getId() {
    return this.id;
  }

  getName() {
    return this.name;
  }

  async getBooks() {
    const books = await this.books.loadItems();
    return books;
  }

  setName(name: string) {
    this.name = name;
  }

  addBook(book: Book) {
    this.books.add(book);
  }
}

Test case

test("one-to-many - if make one book and persist, then book id is defined", async () => {
      // given
      const author = new Author();
      author.setName("test");

      const book1 = new Book();
      book1.setName("book1");
      const book2 = new Book();
      book2.setName("book2");
      const book3 = new Book();
      book3.setName("book3");
      book1.setAuthor(author);
      author.addBook(book1);

      // when
      em.persist(author);
      await em.flush();

      // then
      expect(book1.getId()).toBeDefined();
    });

    test("one-to-many - if make three book and persist, then book ids aren't defined", async () => {
      // given
      const author = new Author();
      author.setName("test");

      const book1 = new Book();
      book1.setName("book1");
      const book2 = new Book();
      book2.setName("book2");
      const book3 = new Book();
      book3.setName("book3");
      book1.setAuthor(author);
      author.addBook(book1);
      book2.setAuthor(author);
      author.addBook(book2);
      book3.setAuthor(author);
      author.addBook(book3);

      // when
      em.persist(author);
      await em.flush();

      // then
      expect(book1.getId()).toBeUndefined();
    });

Expected behavior
I think that ids must be defined in both cases.

Versions

Dependency Version
node 17.7.1
typescript 4.5.2
mikro-orm ^5.1.1
@mikro-orm/mysql ^5.1.1
@B4nan
Copy link
Member

B4nan commented Mar 31, 2022

You are missing wrappedReference: true in your property definition:

  @ManyToOne("Author", { name: "author_id", inversedBy: "books", wrappedReference: true })
  private author: IdentifiedReference<Author>;

@username1103
Copy link
Author

username1103 commented Mar 31, 2022

Yes. I found it. So fix it.
But ids is still undefined. Is there anyting else i missed?

@B4nan
Copy link
Member

B4nan commented Mar 31, 2022

Looks like it is caused by your overridden PK field names.

@B4nan B4nan added the bug Something isn't working label Mar 31, 2022
@B4nan B4nan closed this as completed in 080d8e0 Mar 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants