Skip to content

JoinTable query generates incorrectly when entity doesn't match standard naming conventions #1668

@vados-p7e

Description

@vados-p7e

Steps to reproduce or a small repository showing the problem:

  1. Create two entities that don't follow the naming convention (ex. Thing in table things and Tag in table tags)

  2. Create a join table for them (thing_tags) (note that due to the departure from naming convention earlier, this is also a departure from naming convention)

  3. Create a @ManyToMany relation using @JoinTable inside Thing as follows:

  @ManyToMany(type => Tag, {eager: true})
  @JoinTable({
    name: "thing_tags",
    joinColumn: {name: "thing_id", referencedColumnName: "id"}
    inverseJoinColumn: {name: "tag_id", referencedColumnName: "id"}
  })
  tags: Tag[];

When trying to retrieve a Thing, the query is just barely wrong:

SELECT Thing_tags_relation_id.tag_id AS "tag_id", Thing_tags_relation_id.thing_id AS "thing_id" FROM "tags" "tags" INNER JOIN "thing_tags" "Thing_tags_relation_id" ON (Thing_tags_relation_id.thing_id = $1 AND Thing_tags_relation_id.tag_id = "tags"."id") ORDER BY Thing_tags_relation_id.tag_id ASC, Thing_tags_relation_id.thing_id ASC;
ERROR:  missing FROM-clause entry for table "thing_tags_relation_id"

The problem here is that Thing_tags_relation_id is not quoted everywhere it's mentioned. If I take the query to postgres, and add quotes around every unquoted Thing_tags_relation_id, the query works great:

db_test=# SELECT "Thing_tags_relation_id".tag_id AS "tag_id", "Thing_tags_relation_id".thing_id AS "thing_id" FROM "tags" "tags" INNER JOIN "thing_tags" "Thing_tags_relation_id" ON ("Thing_tags_relation_id".thing_id = 1743 AND "Thing_tags_relation_id".tag_id = "tags". "id") ORDER BY "Thing_tags_relation_id".tag_id ASC, "Thing_tags_relation_id".thing_id ASC;
tag_id | thing_id
--------+-----------
(0 rows)

PS I personally would love a parametrizedQuery parameter in error reports that prints the complete query with params filled out (exactly as it would have been sent).

As always thanks for all the work on typeorm, it is 100% improving the development lives of me and my team :)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions