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

Cannot read properties of undefined (reading '__data') when deleting an entity that uses the mappedBy option of OneToMany as 'owner' #4578

Closed
moowoonsunghoon opened this issue Aug 2, 2023 · 6 comments
Labels
bug Something isn't working

Comments

@moowoonsunghoon
Copy link

Describe the bug
After updating to MikroORM 5.7.14, An error occurs when deleting an entity that uses the mappedBy option of OneToMany as 'owner'

Stack trace
스크린샷 2023-08-02 08 49 08

import { Entity, ManyToOne, PrimaryKey, Property } from '@mikro-orm/core';

import { User } from './user.entity';

@Entity()
export class Team {
  @PrimaryKey({ columnType: 'uuid', defaultRaw: `gen_random_uuid()` })
  id!: string;

  @Property({ columnType: 'uuid', nullable: true })
  userId?: string;

  @ManyToOne(() => User, {
    fieldName: 'user_id',
    onUpdateIntegrity: 'set null',
    onDelete: 'cascade',
    nullable: true,
  })
  owner?: User;
}
import {
  Cascade,
  Collection,
  Entity,
  OneToMany,
  PrimaryKey,
} from '@mikro-orm/core';
import { Team } from './team.entity';

@Entity()
export class User {
  @PrimaryKey({ columnType: 'uuid', defaultRaw: `gen_random_uuid()` })
  id!: string;

  @OneToMany(() => Team, (team) => team.owner, {
    cascade: [Cascade.ALL],
  })
  teams = new Collection<Team>(this);
}

To Reproduce
Steps to reproduce the behavior:

  1. create a OneToMany and ManyToOne relationShip with mappedBy use 'owner' string or entity.owner
  2. Attempt to removeAndFlush entity with One relationship
  3. Throws an error

Expected behavior
Data should be deleted normally.

Versions

Dependency Version
node 16.18.1
typescript 5.1.3
mikro-orm 5.7.14
your-driver "@mikro-orm/postgresql": "^5.7.14"
@B4nan
Copy link
Member

B4nan commented Aug 2, 2023

Can you please provide a complete repro? So in addition to the entity definition, add also few lines that create some data and some that trigger this error. Entity definition does not reproduce much on its own.

@B4nan
Copy link
Member

B4nan commented Aug 2, 2023

I see you are trying to map two persistent properties to a single db column (user and userId), this was never really supported. You should have only one, the relation property - you can access the FK without populating it. Or define the relation as non-persistent.

@moowoonsunghoon
Copy link
Author

@B4nan
Thanks for the quick response!
The same error occurs after removing the userId property.
I've uploaded a reproduction repository
let me know if you have any trouble using it to reproduce the problem. Thanks again!

@B4nan
Copy link
Member

B4nan commented Aug 9, 2023

Ok, this is quite a funny problem - it all boils down to your owning property being called owner which is the same as Collection.owner, an internal property. Because of this match, things got broken.

@B4nan B4nan added the bug Something isn't working label Aug 9, 2023
@B4nan B4nan closed this as completed in fbed4a6 Aug 9, 2023
@moowoonsunghoon
Copy link
Author

moowoonsunghoon commented Aug 14, 2023

@B4nan
Thanks a lot for the quick fix!👍
May I know when the new version will be released?

@B4nan
Copy link
Member

B4nan commented Aug 14, 2023

Use the dev version, I want to ship new feature release next, and still have few things I want to implement first. I hope stable will go out in the end of this week or during next week.

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