Search code examples
mysqltypescriptinner-joinnestjstypeorm

TypeORM-NestJs Join on Json column


join two mysql tables using typeorm-NestJs where table 1(t1) is normal table & table 2(t2) has a column which containts JSON data

now I need to join them based on t1.id == t2.column_name.id

but I am getting error on that query

query I am using :-

const results  = await this.connection.createQueryBuilder(ABCEntity,'t1')
    .innerJoinAndMapMany('t1.Id',xyzEntity,'t2','t1.Id = t2.column_name.$.Id')
    .where('t1.Id = :Id',{Id: id})
    .getMany()
    .catch(err => console.log(err));

Error I am getting :-

code: 'ER_PARSE_ERROR',
  errno: 1064,
  sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.Id WHERE `t1`.`Id` = 'b6D812aF-9e22-4a5a-a292-6rc5021bfv0a'' at line 1",
  sqlState: '42000',


Solution

  • what worked for me was, everything in backticks for eg : `t1.Id = t2.column_name->'$.Id'

    const results  = await this.connection.createQueryBuilder(ABCEntity,'t1')
    .innerJoinAndMapMany('t1.Id',xyzEntity,'t2',`t1.Id = t2.column_name->'$.Id'`)
    .where('t1.Id = :Id',{Id: id})
    .getMany()
    .catch(err => console.log(err));