Repository navigation
Closed
Labels
Description
Issue Description
Expected Behavior
The find method filters by the entries on which the connected entities match.
Actual Behavior
The find method tries to filter by the foreign key and does not even pass a value.
When using find() on a foreign key and passing an object into a foreign key property of the where object the generated SQL is completely wrong.
Given 2 Entities:
@Entity("entity1")
export class Entity1 {
@PrimaryGeneratedColumn("uuid")
@PrimaryColumn("uuid")
id!: string;
@ManyToOne(() => Entity2, entity2 => entity2.id)
entity2: Entity2;
@Column()
foo: string;
}and
@Entity("entity2")
export class Entity2 {
@PrimaryGeneratedColumn("uuid")
@PrimaryColumn("uuid")
id!: string;
@Column({
unique: true
})
name: string;
}as well as the fetching code:
async function fetch(foo: string, name: string) {
const connection = await createConnection({
host: "XXXX",
database: "XXXX",
port: 1234,
type: "postgres",
username: "XXXX",
password: "XXXX",
entities: [
Entity1,
Entity2
]
});
const entity1Repository = connection.getRepository(Entity1);
const results = await entity1Repository.find({
where: {
entity2: {
name
},
foo
}
});
console.log(results);
}It should create something like this:
SELECT
"Entity1"."id" AS "Entity1_id",
"Entity1"."foo" AS "Entity1_foo",
"Entity1"."entity2Id" AS "Entity1_entity2Id",
"Entity1_entity2"."id" as "Entity1_entity2_id",
"Entity1_entity2"."name" AS "Entity1_entity2_name"
FROM
"entity1" AS "Entity1"
JOIN
"Entity2" AS "Entity1_entity2" ON
"Entity1_entity2"."id" = "Entity1"."entity2Id"
WHERE
"Entity1"."foo" = $1 AND
"Entity1_entity2"."name" = $2 -- Parameters: ["foo", "bar"]However, the generated code is:
SELECT
"Entity1"."id" AS "Entity1_id",
"Entity1"."foo" AS "Entity1_foo",
"Entity1"."entity2Id" AS "Entity1_entity2Id",
"Entity1_entity2"."id" as "Entity1_entity2_id",
"Entity1_entity2"."name" AS "Entity1_entity2_name"
FROM
"entity1" AS "Entity1"
JOIN
"Entity2" AS "Entity1_entity2" ON
"Entity1_entity2"."id" = "Entity1"."entity2Id"
WHERE
"Entity1"."foo" = $1 AND
-- Now comes the mistake:
"Entity1_entity2Id" = $2 -- Parameters: ["foo", null]which of course does not work.
Steps to Reproduce
- create above code
- run it
- fail
My Environment
I am using this inside NestJS (which I have trimmed out in the above example for the sake of simplicity)
| Dependency | Version |
|---|---|
| Operating System | Linux |
| Node.js version | v15.14.0 |
| Typescript version | v4.2.3 |
| TypeORM version | v0.2.32 |
Additional Context
Relevant Database Driver(s)
-
aurora-data-api -
aurora-data-api-pg -
better-sqlite3 -
cockroachdb -
cordova -
expo -
mongodb -
mysql -
nativescript -
oracle -
postgres -
react-native -
sap -
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.
- Yes, I have the time, but I don't know how to start. I would need guidance.
- No, I don't have the time, although I believe I could do it if I had the time...
- No, I don't have the time and I wouldn't even know how to start.
edit: formatted SQL code and inserted comment to better point out the error.
Reacted by Skynightz93
Activity
LuisPeregrinaPSL commentedon Jun 14, 2021
Most likely the same thing I'm having:
The end part of the generated query is:
WHERE "SkillRecord"."userId" = $1 AND "SkillRecord"."skillId" = $2 -- PARAMETERS: ["abc123",null]imnotjames commentedon Jul 3, 2021
This is fixed in #7805