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

Can't search by nested full-text columns in a Postgres db #3696

Closed
imdeaconu opened this issue Nov 5, 2022 · 1 comment
Closed

Can't search by nested full-text columns in a Postgres db #3696

imdeaconu opened this issue Nov 5, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@imdeaconu
Copy link

Describe the bug

You can't search by full-text columns in nested entities.

Stack trace

node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^

DriverException: Cannot read properties of undefined (reading 'columnTypes')
    at PostgreSqlExceptionConverter.convertException (D:\mikroorm-bug\node_modules\@mikro-orm\core\platforms\ExceptionConverter.js:8:16)
    at PostgreSqlExceptionConverter.convertException (D:\mikroorm-bug\node_modules\@mikro-orm\postgresql\PostgreSqlExceptionConverter.js:42:22)
    at PostgreSqlDriver.convertException (D:\mikroorm-bug\node_modules\@mikro-orm\core\drivers\DatabaseDriver.js:192:54)
    at D:\mikroorm-bug\node_modules\@mikro-orm\core\drivers\DatabaseDriver.js:196:24
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async PostgreSqlDriver.find (D:\mikroorm-bug\node_modules\@mikro-orm\knex\AbstractSqlDriver.js:49:24)
    at async SqlEntityManager.find (D:\mikroorm-bug\node_modules\@mikro-orm\core\EntityManager.js:106:25)
    at async main (D:\mikroorm-bug\build\index.js:88:21)

previous TypeError: Cannot read properties of undefined (reading 'columnTypes')
    at PostgreSqlPlatform.getFullTextWhereClause (D:\mikroorm-bug\node_modules\@mikro-orm\postgresql\PostgreSqlPlatform.js:65:18)
    at QueryBuilderHelper.processObjectSubCondition (D:\mikroorm-bug\node_modules\@mikro-orm\knex\query\QueryBuilderHelper.js:384:47)
    at QueryBuilderHelper.appendQuerySubCondition (D:\mikroorm-bug\node_modules\@mikro-orm\knex\query\QueryBuilderHelper.js:334:25)
    at D:\mikroorm-bug\node_modules\@mikro-orm\knex\query\QueryBuilderHelper.js:325:18
    at Array.forEach (<anonymous>)
    at QueryBuilderHelper.appendQueryCondition (D:\mikroorm-bug\node_modules\@mikro-orm\knex\query\QueryBuilderHelper.js:314:27)
    at D:\mikroorm-bug\node_modules\@mikro-orm\knex\query\QueryBuilderHelper.js:526:29
    at Array.forEach (<anonymous>)
    at QueryBuilder_PostgreSQL.<anonymous> (D:\mikroorm-bug\node_modules\@mikro-orm\knex\query\QueryBuilderHelper.js:520:42)
    at compileCallback (D:\mikroorm-bug\node_modules\knex\lib\formatter\formatterUtils.js:7:12)  

To Reproduce

import { Collection, Index, ManyToMany, MikroORM } from "@mikro-orm/core";

import { Entity, PrimaryKey, Property, Unique } from "@mikro-orm/core";
import { FullTextType } from "@mikro-orm/postgresql";

@Entity()
@Unique({ properties: ["name"] })
export class Artist {
  @PrimaryKey({ type: "uuid", defaultRaw: "uuid_generate_v4()" })
  id!: string;

  @Property()
  name: string;

  @Index({ type: "fulltext" })
  @Property({ type: FullTextType, onUpdate: (artist: any) => artist.name })
  sercheableName!: string;

  constructor(artist: any) {
    this.id = artist.id;
    this.name = artist.name;
    this.sercheableName = artist.name;
  }
}
@Entity()
export class Song {
  @PrimaryKey({ type: "uuid", defaultRaw: "uuid_generate_v4()" })
  id!: string;

  @Property()
  title: string;

  @ManyToMany(() => Artist)
  artists = new Collection<Artist>(this);

  @Index({ type: "fulltext" })
  @Property({ type: FullTextType, onUpdate: (song: any) => song.title })
  sercheableTitle!: string;

  constructor(song: any) {
    this.id = song.id;
    this.title = song.title;
    this.sercheableTitle = song.title;
  }
}

const main = async () => {
  const orm = await MikroORM.init();
  const em = orm.em.fork();
  const artist = await em.create(Artist, {
    name: "Taylor Swift",
    sercheableName: "Taylor Swift",
  });
  em.persist(artist);
  const song = await em.create(Song, {
    title: "Anti-Hero",
    sercheableTitle: "Anti--Hero",
  });
  song.artists.add(artist);
  em.persist(song);
  await em.flush();
  const query = "anti";
  const results = await em.find(Song, {
    $or: [
      { sercheableTitle: { $fulltext: query } },
      { artists: { sercheableName: { $fulltext: query } } },
    ],
  });
  console.log(results);
};

main();

Expected behavior

You should be able to search by proprieties of nested entities.

Additional context
Searching by non-nested properties seems to be fine.

Versions

Dependency Version
node v16.17.0
typescript 4.8.4
mikro-orm 5.5.0
@mikro-orm/postgresql 5.5.0
@B4nan
Copy link
Member

B4nan commented Nov 5, 2022

Thanks for the repro.

FYI since v5.5 you don't need to call em.persist() on entities created via em.create(), also em.create() is sync method, no need to await it.

@B4nan B4nan added the bug Something isn't working label Nov 5, 2022
@B4nan B4nan closed this as completed in 9a2f535 Nov 5, 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