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

Relations inside more than one nested embedded with select throws error #10821

Open
mohaalak opened this issue Apr 9, 2024 · 0 comments
Open

Comments

@mohaalak
Copy link

mohaalak commented Apr 9, 2024

Issue description

if you have more than one embeded nested in each other and in the nested embeded and you select it it will throw error that it cannot find the selection.

Expected Behavior

@Entity()
export class Image {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column()
  title!: string;

  @Column()
  file!: string;
}

class Media {
  @ManyToMany(() => Image)
  @JoinTable()
  images!: Image[];
}

class Body {
  @Column()
  text!: string;

  @Column(() => Media)
  media!: Media;
}
@Entity()
export class Post {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column()
  title!: string;

  @Column(() => Body)
  body!: Body;
}


async function init() {
  const AppDataSource = new DataSource({
    type: "sqlite",
    database: "test.sqlite",
    synchronize: true,
    entities: [Image, Post],
    logging: true,
    migrations: [],
  });

  await AppDataSource.initialize();

  const postRepo = AppDataSource.getRepository(Post);

  await postRepo.find({
    relations: {
      body: {
        media: {
          images: true,
        },
      },
    },
    select: {
      title: true,
      body: {
        media: {
          images: true,
        },
      },
    },
  });
}

init();

this should work and return an empty array.

Actual Behavior

but it will raise an error

~/typeorm_test/node_modules/.pnpm/typeorm@0.3.20_sqlite3@5.1.6_ts-node@10.9.1/node_modules/src/util/OrmUtils.ts:225
            obj = obj[segments[i]]
                     ^
TypeError: Cannot read properties of undefined (reading 'media')
    at Function.deepValue (~/typeorm_test/node_modules/.pnpm/typeorm@0.3.20_sqlite3@5.1.6_ts-node@10.9.1/node_modules/src/util/OrmUtils.ts:225:22)
    at ~/typeorm_test/node_modules/.pnpm/typeorm@0.3.20_sqlite3@5.1.6_ts-node@10.9.1/node_modules/src/query-builder/SelectQueryBuilder.ts:3938:36
    at Array.forEach (<anonymous>)
    at SelectQueryBuilder.buildRelations (~/typeorm_test/node_modules/.pnpm/typeorm@0.3.20_sqlite3@5.1.6_ts-node@10.9.1/node_modules/src/query-builder/SelectQueryBuilder.ts:3924:32)
    at ~/typeorm_test/node_modules/.pnpm/typeorm@0.3.20_sqlite3@5.1.6_ts-node@10.9.1/node_modules/src/query-builder/SelectQueryBuilder.ts:3935:22
    at Array.forEach (<anonymous>)
    at SelectQueryBuilder.buildRelations (~/typeorm_test/node_modules/.pnpm/typeorm@0.3.20_sqlite3@5.1.6_ts-node@10.9.1/node_modules/src/query-builder/SelectQueryBuilder.ts:3924:32)
    at SelectQueryBuilder.applyFindOptions (~/typeorm_test/node_modules/.pnpm/typeorm@0.3.20_sqlite3@5.1.6_ts-node@10.9.1/node_modules/src/query-builder/SelectQueryBuilder.ts:3113:22)
    at SelectQueryBuilder.setFindOptions (~/typeorm_test/node_modules/.pnpm/typeorm@0.3.20_sqlite3@5.1.6_ts-node@10.9.1/node_modules/src/query-builder/SelectQueryBuilder.ts:105:14)
    at EntityManager.find (~/typeorm_test/node_modules/.pnpm/typeorm@0.3.20_sqlite3@5.1.6_ts-node@10.9.1/node_modules/src/entity-manager/EntityManager.ts:1104:14)
    ```

### Steps to reproduce

you can copy paste the whole code from expected behaviour.

### My Environment


| Operating System    |          |
| Node.js version     | 18.19.0  |
| Typescript version  | 5.0.4 |
| TypeORM version     | 0.3.20 |


### Additional Context

_No response_

### Relevant Database Driver(s)

- [ ] aurora-mysql
- [ ] aurora-postgres
- [ ] better-sqlite3
- [ ] cockroachdb
- [ ] cordova
- [ ] expo
- [ ] mongodb
- [ ] mysql
- [ ] nativescript
- [ ] oracle
- [ ] postgres
- [ ] react-native
- [ ] sap
- [ ] spanner
- [ ] sqlite
- [ ] sqlite-abstract
- [ ] sqljs
- [ ] sqlserver

### Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, and I know how to start.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant