You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When trying to query an entity by adding a condition on a JsonType field for a related 1:N entity, Mikro-ORM will generate a broken query.
To Reproduce
import'reflect-metadata';import{Collection,Entity,JsonType,ManyToOne,MikroORM,OneToMany,PrimaryKey,Property,wrap}from"@mikro-orm/core";import{PostgreSqlDriver}from'@mikro-orm/postgresql';
@Entity()exportclassAuthor{
@PrimaryKey()id!: string;
@OneToMany({entity: ()=>Book,mappedBy: 'author',})books=newCollection<Book>(this);}
@Entity()exportclassBook{
@PrimaryKey()id!: string;
@Property({type: JsonType})data!: Record<string,unknown>;
@ManyToOne({entity: ()=>Author,inversedBy: 'books',})author!: Author;}conststart=async()=>{constdb=awaitMikroORM.init<PostgreSqlDriver>({entities: [Book,Author],type: 'postgresql',dbName: 'debug',host: 'localhost',port: 5432,user: 'postgres',password: 'postgres',debug: true,});// Faulty:constquery1=awaitdb.em.fork().find(Author,{books: {data: {title: 'test'}},});// Query output:// select "a0".* from "author" as "a0" left join "book" as "b1" on "a0"."id" = "b1"."author_id" where b1."b1"."data"->>'title' = 'test'// Observe how the data property condition is prefixed with a double table alias.// Correct:constquery2=awaitdb.em.fork().find(Author,{books: {id: 'test'},});// Query output:// select "a0".* from "author" as "a0" left join "book" as "b1" on "a0"."id" = "b1"."author_id" where "b1"."id" = 'test'// Also correct:constquery3=awaitdb.em.fork().find(Book,{data: {title: 'test'},});// Query output:// select "b0".* from "book" as "b0" where "b0"."data"->>'title' = 'test'}start();
Expected behavior
Mikro-ORM will generate a working query for query 1. Specifically, in the WHERE clause the "data" column will have a single alias.
Actual behavior
Mikro-ORM generates a WHERE clause containing double table aliases. Postgres (and probably other databases too) will generate an error: TableNotFoundException: select "a0".* from "author" as "a0" left join "book" as "b1" on "a0"."id" = "b1"."author_id" where b1."b1"."data"->>'title' = 'test' - invalid reference to FROM-clause entry for table "b1"
Additional context
Add any other context about the problem here.
Versions
Dependency
Version
node
v17.4.0
typescript
4.7.3
mikro-orm
5.2.0
postgresql
5.2.0
The text was updated successfully, but these errors were encountered:
Describe the bug
When trying to query an entity by adding a condition on a JsonType field for a related 1:N entity, Mikro-ORM will generate a broken query.
To Reproduce
Expected behavior
Mikro-ORM will generate a working query for query 1. Specifically, in the WHERE clause the "data" column will have a single alias.
Actual behavior
Mikro-ORM generates a WHERE clause containing double table aliases. Postgres (and probably other databases too) will generate an error: TableNotFoundException: select "a0".* from "author" as "a0" left join "book" as "b1" on "a0"."id" = "b1"."author_id" where b1."b1"."data"->>'title' = 'test' - invalid reference to FROM-clause entry for table "b1"
Additional context
Add any other context about the problem here.
Versions
The text was updated successfully, but these errors were encountered: